urllib.request.urlretrieve () for downloading content to develop local url

Function: urllib.urlretrieve (url [, filename [  , reporthook [, data]]])
Parameters: 
URL: external or local URL 
filename: Specifies the path to the local storage (if the parameter is not specified, urllib generates a temporary to save the data file); 
reporthook: is a callback function, when connected to the server, and the corresponding data block transfer will trigger the callback is complete. We can use this callback function to display the current download progress. 
data: refers to post the data server. This method returns a tuple (filename, headers) contains two elements, filename indicates saved to a local path, header represents the response of the server header. 

Examples of Baidu crawl home to save as follows:

. 1  from the urllib Import Request
 2  DEF Fun (blocknum Use, BLOCKSIZE, totalSize):
 . 3      "" " 
. 4      blocknum Use: The current block number
 . 5      BLOCKSIZE: each transmission block size
 . 6      totalSize: total size of page file
 . 7      " "" 
. 8      Percent = * BLOCKSIZE blocknum Use / totalSize
 . 9      IF Percent> 1.0 :
 10          Percent = 1.0
 . 11      Percent Percent = 100 *
 12 is      Print ( " downloads:% .2f %% " % (Percent))
 13 is URL = "http://www.baidu.com"
14 path = r"C:\Users\Administrator\Desktop\download\sina.html"
15 request.urlretrieve(url, path, fun)

 

Guess you like

Origin www.cnblogs.com/greatljg/p/11067913.html