Python3 crawler Download Images FAQ

Source: Internet
Author: User

Just started to learn Python3, Baidu, the use of Python, see the crawler download pictures is very fun, so Baidu all kinds of information learning, the results are always running unsuccessful, finally changed, recorded.

From urllib Import requestImportUrllibImportRedefgethtml (URL): page=Urllib. request. Urlopen (URL) HTML=Page.read ()returnHTMLdefgetimg (HTML): Reg= R'src= "(. +?\.jpg)"'Imgre=re.compile (reg) HTML=html.decode ('Utf-8')#do not write this line will error: Typeerror:cannot use a string pattern on a Bytes-like objectImglist =Re.findall (imgre,html) x= 1 forImgurlinchImglist:urllib. request. Urlretrieve (Imgurl,'c:/users/zx/desktop/images/%s.jpg'%x) x+=1returnimglisthtml= Gethtml ("http://www.win4000.com/wallpaper_2358_0_10_1.html")Print(getimg (HTML))

Red is the difference between Python3 and Python2, if not written will be error: Attributeerror:module ' urllib ' has no attribute ' Urlopen '

If [] is returned, the regular expression may be incorrect

Python3 crawler Download Images FAQ

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.