[Example of urllib use of turn]python3.x

Source: Internet
Author: User

The 2.x version of Python can be manipulated directly using the import Urllib, but the 3.x version of Python uses the import urllib.request for operation, here is a simple example:

===============================================================================

# get code of given URL as HTML text string
# Python3 uses Urllib.request.urlopen ()
# instead of Python2 ' s Urllib.urlopen () or Urllib2.urlopen ()

Import Urllib.request

fp = Urllib.request.urlopen ("http://www.python.org")

Mybytes = Fp.read ()
# Note that Python3 does not read the HTML code as String
# But as an HTML code bytearray, convert to string with
MyStr = Mybytes.decode ("UTF8")

Fp.close ()

Print (MYSTR)

================================================================================

# Get the code of a given URL as HTML text string
# Python3 uses Urllib.request.urlopen ()
# get the encoding used first
# Tested with Python 3.1 with the Editra IDE

Import Urllib.request

def extract (text, Sub1, SUB2):
"""
Extract a substring from text between first
Occurances of substrings sub1 and SUB2
"""
Return Text.split (SUB1, 1) [ -1].split (SUB2, 1) [0]


fp = Urllib.request.urlopen ("http://www.python.org")

Mybytes = Fp.read ()

encoding = Extract (str (mybytes). Lower (), ' charset= ', ' "')
Print ('-' *50)
Print ("Encoding type =%s"% Encoding)
Print ('-' *50)

If encoding:
# Note that Python3 does not read the HTML code as String
# But as an HTML code bytearray, convert to string with
MyStr = Mybytes.decode (encoding)
Print (MYSTR)
Else
Print ("Encoding type not found!")


Fp.close ()

==================================================================================

Reference: http://www.daniweb.com/forums/thread213221.html

[Example of urllib use of turn]python3.x

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.