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