Method 1.urllib.urlopen (Url[,data[,proxies]) in the Urllib module
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 urllib>>> f = urllib.urlopen (' http://www.google.com.hk/') >>> firstline = F.readline () #读取html页面的第一行 >>> firstline ' <!doctype 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 ' >>>> filename[0] '/tmp/tmp8evljq ' >>> filename[1]Save As local file:
>>> filename = urllib.urlretrieve (' http://www.google.com.hk/', filename= '/home/dzhwen/python file/homework/ Urllib/google.html ') >>> type (filename) <type ' tuple ' >>>> filename[0] '/home/dzhwen/python\ Xe6\x96\x87\xe4\xbb\xb6/homework/urllib/google.html ' >>> filename[1]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 ') ' http%3a//www.baidu.com ' >>> urllib.quote_plus (' HTTP// Www.baidu.com ') ' 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 urllib>>> params=urllib.urlencode ({' spam ': 1, ' eggs ': 2, ' bacon ': 0}) >>> params ' Eggs=2&bacon=0&spam=1 ' >>> f=urllib.urlopen ("http://python.org/query?%s"% params) >>> Print F.read ()
Post method:
>>> import urllib>>> Parmas = Urllib.urlencode ({' spam ': 1, ' eggs ': 2, ' bacon ': 0}) >>> f= Urllib.urlopen ("Http://python.org/query", Parmas) >>> F.read ()
Basically, the method of object acquisition is not discussed.
Python urllib Library