Python urllib Library

Source: Internet
Author: User

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

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.