python爬蟲---爬取想要的圖片

#coding:utf-8

import re
import requests

url='http://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1460997499750_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=哆啦A夢'
#想要爬取哪種圖片就可以更改word後的值(即標紅的位置)
html=requests.get(url).text
print(html)
pic_url=re.findall('"objURL":"(.*?)",',html,re.S)
i=0;
for each in pic_url:
    print(each)
    try:
        pic=requests.get(each,timeout=10)
    except requests.exceptions.ConnectionError:
        print('[錯誤]當前圖片無法下載')
        continue
    string='D:\pythonPro\studyPro\img\\'+str(i)+'.jpg'
    fp=open(string,'wb')
    fp.write(pic.content)
    fp.close()
    i+=1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章