Urlretrieve functions in Python

Source: Internet
Author: User

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

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.