1. Urllib module 1.urllib.urlopen (url[,data[,proxies])
Opens a URL method that returns a file object, which can then be manipulated like a file object. This example tries to open Google
Import URLLIBF = Urllib.urlopen (' http://www.google.com.hk/') Firstline = F.readline () #读取html页面的第一行
Urlopen provides methods for returning objects:
-Read (), ReadLine (), ReadLines (), Fileno (), Close (): These methods are used exactly like file objects
-INFO (): Returns a httplib. Httpmessage object that represents the header information returned by the remote server
-GetCode (): Returns the HTTP status code. If it is an HTTP request, 200 request completed successfully; 404 URL not Found
-Geturl (): Returns the requested URL
2.urllib.urlretrieve (Url[,filename[,reporthook[,data]])
The Urlretrieve method downloads the HTML file where the URL is located to your local hard drive. If you do not specify filename, it is saved as a temporary file.
Urlretrieve () returns a two-tuple (Filename,mine_hdrs)
Temporary storage:
filename = Urllib.urlretrieve (' http://www.google.com.hk/') type (filename) <type ' tuple ' >print filename[0]print FILENAME[1]
Output:
'/tmp/tmp8evljq '
Save As local file:
filename = Urllib.urlretrieve (' http://www.baidu.com/', filename= '/home/dzhwen/python file/homework/urllib/ Google.html ') print type (filename) print filename[0]print filename[1]
Output:
<type ' tuple ' > '/home/dzhwen/python\xe6\x96\x87\xe4\xbb\xb6/homework/urllib/google.html '
3.urllib.urlcleanup ()Clears the cache generated by Urllib.urlretrieve ()
4.urllib.quote (URL) and Urllib.quote_plus (URL)The URL data is obtained after it is encoded and thus applied with the URL string so that it can be printed and accepted by the Web server.
Urllib.quote (' http://www.baidu.com ')
Conversion Result:
' Http%3a//www.baidu.com '
Urllib.quote_plus (' http://www.baidu.com ')
Conversion Result:
' Http%3a%2f%2fwww.baidu.com '
5.urllib.unquote (URL) and Urllib.unquote_plus (URL)Contrary to the 4 function.
6.urllib.urlencode (query)Assign key-value pairs in URLs to connectors & divisions
This can be combined with urlopen to implement the Post method and the Get method:
Get method:
Import urllibparams=urllib.urlencode ({' spam ': 1, ' eggs ': 2, ' bacon ': 0}) F=urllib.urlopen ("http://python.org/query?%s "% params" Print f.read ()
Post method:
Import Urllibparmas = Urllib.urlencode ({' spam ': 1, ' eggs ': 2, ' bacon ': 0}) F=urllib.urlopen ("Http://python.org/query", Parmas) F.read ()
2, Urllib2 please refer to:Http://www.cnblogs.com/yuxc/archive/2011/08/01/2123995.html
Python urllib, urllib2 module explanation