Urlopen () &urlretrieve ()

Source: Internet
Author: User

1. Urlopen () method

Urllib.request.urlopen (Url[,data[,proxies])

Create a class file object that represents a remote URL, and then manipulate the class file object like a local file to get remote data

The parameter URL represents the path to the remote data, usually the URL

The parameter data represents a post-submission to a URL (the person who played the web should only have two ways of submitting the data: Post and get. If you are not clear, do not care, because this parameter is rarely used in general)

The parameter proxies is used to set the proxy.

Urlopen () Returns a class file object that provides the following methods:

Read (), ReadLine (), ReadLines (), Fileno (), Close (): These methods are used in exactly the same way as 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 means the request is successful, 404 means the URL is not found

Geturl (): Returns the requested URL

2. Urlretrieve method

Download remote data directly to a local

Urllib.request.urlretrieve (Url[,filename[,reporthook[,data]])

Parameter description

URL: external or local URL

FileName: The path to the local is established, (if this parameter is not specified, Urllib generates a temporary file to hold the data)

Reporthook: is a callback function that triggers the callback function when the server is connected and the data module of the response is transmitted, and we can use this callback function to display the current download progress

Data: Refers to a post to the server. The method returns a Ganso (filename,headers) filename containing two elements that represents the path saved to the local, headers represents the server response header.

Urlopen () &urlretrieve ()

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.