Python urllib, urllib2 module explanation

Source: Internet
Author: User
Tags urlencode

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 ('') 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 ('') type (filename) <type ' tuple ' >print filename[0]print FILENAME[1]


'/tmp/tmp8evljq ' 

Save As local file:

filename = Urllib.urlretrieve ('', filename= '/home/dzhwen/python file/homework/urllib/ Google.html ') print type (filename) print filename[0]print filename[1]


<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 (' ')

Conversion Result:

' Http%3a// '
Urllib.quote_plus (' ')

Conversion Result:

' '
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 (" "% params" Print ()

Post method:  

Import Urllibparmas = Urllib.urlencode ({' spam ': 1, ' eggs ': 2, ' bacon ': 0}) F=urllib.urlopen ("Http://", Parmas) ()
2, Urllib2 please refer to:









Python urllib, urllib2 module explanation

Related Article

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: 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.