API definition: Urllib.request.urlretrieve (url,filename=none,reporthook=none, Data=none) uses Urlretrieve () to download data locally. -The parameter finename specifies the save local path (Urllib generates a temporary file to save the data if the parameter is not specified.) )-Parameter reporthook is a callback function that triggers the callback when the server is connected and the corresponding data block is transferred, and we can use this callback function to display the current download progress. -parameter data refers to a post to the server, the method returns a two-element (filename, headers) tuple, filename represents the path to the local, and the header represents the server's response header. Usage:
Import urllib.request>>>local_filename,headers=urllib.request.urlretrieve ('http// python.org/')>>> html = Open (local_filename)>>> html.close ()
Note The Unicode error handling method may occur when Html=open (local_filename) and then Lines=html.readlines (): Html=open (local_filename, ' utf-8 ' This resolves the Unicode issue. Example: Crawling a Web page
#Coding:utf-8 fromUrllib.requestImportUrlretrievedefFirstnonblank (lines): forEachlineinchlines:if notEachline.strip ():Continue Else: returnEachlinedeffirstlast (webpage): F=open (webpage,encoding='Utf-8') Lines=f.readlines () f.close ( )Print(Firstnonblank (lines)) Lines.reverse ()Print(Firstnonblank (lines))defDownload (url='http://www.baidu.com', process=firstlast):Try: retval=urlretrieve (URL) [0]exceptIoerror:retval=Noneifretval:process (retval)if __name__=="__main__": Download ()
Urlretrieve functions in Python