"Python" python3 implement web crawler download image

Source: Internet
Author: User

ImportReImporturllib.request#------ways to get Web page source code---defgethtml (URL): page=urllib.request.urlopen (URL) HTML=Page.read ()returnHTML#Enter the URL of any post------gethtml ()------html = gethtml ("https://tieba.baidu.com/p/5352556650")#------Modify the character encoding within the HTML object to UTF-8------html = Html.decode ('UTF-8')#------How to get all the picture addresses in a post------defgetimg (HTML):#------Use regular expressions to match Web page content to find a picture address------Reg = R'src= "([. *\s]*\.jpg)"'Imgre=Re.compile (REG); Imglist=Re.findall (imgre, HTML)returnimglistimglist=getimg (HTML) imgname=0 forImgpathinchimglist:#------It is best to use exception handling and multithreaded programming------    Try: F= Open ('d:\\temp\\'+ STR (imgname) +". jpg",'WB') F.write ((Urllib.request.urlopen (Imgpath)). Read ())Print(Imgpath) f.close ()exceptException as E:Print(imgpath+"Error") Imgname+ = 1Print("All done!")

"Python" python3 implement web crawler download image

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.