Python爬取百度图片

注意:此代码只能爬取30张左右,如果有朋友能告知怎么提高张数,感激不尽!

import re
import requests
import urllib.request

# 非正经图片,请注意(我真的是为了学术!!!)
req = urllib.request.urlopen('https://image.baidu.com/search/index?ct=201326592&cl=2&st=-1&lm=-1&nc=1&ie=utf-8&tn=baiduimage&ipn=r&rps=1&pv=&fm=rs1&word=%E7%BD%91%E7%BB%9C%E7%BE%8E%E5%A5%B3&oriquery=%E7%BE%8E%E5%A5%B3&ofr=%E7%BE%8E%E5%A5%B3&sensitive=0&z=0&ic=0&width=0&height=0&latest=0&hd=1©right=0')
buf = req.read()

#"thumbURL":"http://img1.imgtn.bdimg.com/it/u=4211603615,2895194094&fm=26&gp=0.jpg
listurl = re.findall(r'"thumbURL":".+?\.jpg',buf.decode('utf-8'))
print(listurl)
strurl = re.sub(r'thumbURL":"','',str(listurl))
strurl = strurl.replace('[','')
strurl = strurl.replace(']','')
strurl = strurl.replace(' ','')
strurl = strurl.replace('\',\'','zhangru')
strurl = strurl.replace('\'','')
listurl = re.split(r'zhangru',strurl)


i = 1
for url in listurl:
    print(url)
    f = open('picnew\\'+str(i)+'.jpg','wb')
    response = urllib.request.urlopen(url)
    buf = response.read()
    f.write(buf)
    i += 1

你可能感兴趣的:(Python学习初涉)