python爬虫抓取淘宝图片
生活随笔
收集整理的這篇文章主要介紹了
python爬虫抓取淘宝图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
訓練模型需要data,于是從網上抓取相關圖片集,之后進行篩選標注。
import urllib.request import rekeyword = '騷豬'#定義搜索關鍵字 keyword = urllib.request.quote(keyword)#對關鍵字編碼headers = ('User-Agent',"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36") opener = urllib.request.build_opener() opener.addheaders = [headers] urllib.request.install_opener(opener)for i in range(0, 20):#設定爬取頁數0到20頁#淘寶搜索頁面規律url = 'https://s.taobao.com/search?q='+keyword+'&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&bcoffset=4&ntoffset=4&p4ppushleft=1%2C48&s='+str(i*44)data = urllib.request.urlopen(url).read().decode('utf-8', 'ignore')pat = 'pic_url":"//(.*?)"'imagelist = re.compile(pat).findall(data)for j in range(0, len(imagelist)):thisimage = imagelist[j]thisurl = 'http://'+thisimagefile = 'E:/datatb/tshirt/tshirt'+str(i)+str(j)+'.jpg'#保存圖片位置urllib.request.urlretrieve(thisurl,file)建議一次獲取頁面不要太多,不然會請求失敗。
總結
以上是生活随笔為你收集整理的python爬虫抓取淘宝图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 经典智力题:小明一家人过桥问题
- 下一篇: PCB设计中的正片和负片设计原理