using the Urllib.request and re modules
1 fromUrllib.requestImport*2 ImportRe#Handling Network Access3 #get Web page4URL ='Https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk&word =%c3%c0%c5%ae%cd%bc%c6%ac&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=111111'5 #Open Web page6HMTL =urlopen (URL)7 #get HTML code, decode decode8obj =Hmtl.read (). Decode ()9 #use RE to find all the Objurl links. * Match all resultsTenURLs = Re.findall (r'" Objurl": "(. *?)"', obj) Oneindex = 1 A forwr.inchURLs: - Try: - ifRe.search ('. jpg$', URL): the Print('downloading........%d'%index) -Urlretrieve (URL,'pic'+STR (Index) +'. jpg') - Else: - Print('downloading........%d'%index) +Urlretrieve (URL,'pic'+ STR (index) +'. PNG') -Index + = 1 + A exceptException: at Print('Download error....%d'%index) - Else: - Print('Download complete')
Crawl a picture
Using the Requests module
1 Import requests2'http://www.cnblogs.com/Images/Skins/BJ2008.jpg' 3 response = Requests.get (image_url)4 with open ('outlook.jpg ','wb') as F:5 f.write (response.content)
Python Simple crawler