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