"Python" downloads remote content to local

Source: Internet
Author: User

Source: http://www.jb51.net/article/42630.htm

urllib Module urlretrieve method

Urllib.urlretrieve (url[, filename[, reporthook[, data]])
Parameter description:
URL: external or local URL
FileName: Specifies the path to be saved locally (if this parameter is not specified, Urllib generates a temporary file to hold the data);
Reporthook: is a callback function that triggers the callback when the server is connected and the corresponding data block is transferred. We can use this callback function to display the current download progress.
Data: Refers to a post to the server. The method returns a tuple of two elements (filename, headers), filename, which represents the local path, and the header represents the server's response header. Example: The following example to illustrate the use of this method, this example of the Sina homepage of the HTML crawl to local, saved in the d:/sina.html file, while showing the progress of the download.
ImportUrllibdefCallbackfunc (Blocknum, BlockSize, totalsize):" "callback function @blocknum: the data block @blocksize: The size of the data block @totalsize: The size of the remote file" "percent= 100.0 * Blocknum * blocksize/totalsizeifPercent > 100: Percent= 100Print "%.2f%%"%Percenturl='http://www.sina.com.cn'Local='d:\\sina.html'urllib.urlretrieve (URL, local, callbackfunc)

"Python" downloads remote content to local

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