国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python 爬蟲一鍵爬取 淘寶天貓寶貝頁面主圖顏色圖和詳情圖的教程

2020-01-04 14:59:58
字體:
來源:轉載
供稿:網友

實例如下所示:

import requestsimport re,sys,osimport jsonimport threadingimport pprintclass spider: def __init__(self,sid,name):  self.id = sid self.headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",  "Accept-Encoding":"gzip",  "Accept-Language":"zh-CN,zh;q=0.8",  "Referer":"http://www.example.com/",  "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"  }  self.name=name def openurl(self,url):  self.request = requests.get(url,headers = self.headers)  if self.request.ok:  return self.request.text   def matchs(self):  tmall_exp = r"Setup/(([/s/S]+?)/);"### 匹配商品數據的正則 detail= r"src=/"(https://img/S+?[jpgifn]+?)/"" ###匹配 商品詳情圖的正則 html = self.openurl("https://detail.tmall.com/item.htm?id=%s"%self.id) data = re.findall(tmall_exp,html) data = json.loads(data[0]) main_img = data['propertyPics'] ## 這里包括了主圖和顏色圖的地址 color_data =data['valItemInfo'] ['skuList'] ### 這里獲得商品的顏色信息列表 包括顏色編碼 顏色名稱,商品skuID detail_html = self.openurl("http:"+data['api']["httpsDescUrl"]) detail_image = re.findall(detail,detail_html) self.newdata={"MAIN":main_img['default'],"DETAIL":detail_image,"id":self.id,}  psvs = [] self.newdata['COLOR']=[]  for v in range(len(color_data)):  if ";"in color_data[v]["pvs"]:  psv = color_data[v]['pvs'][color_data[v]['pvs'].find(";")+1:]  else:  psv = color_data[v]['pvs']  if psv in psvs:    continue  psvs.append(psv)   self.newdata['COLOR'].append({color_data[v]["names"]:main_img[";"+psv+";"]})   pprint.pprint(self.newdata)  return self.newdata  def download(self): if len(self.newdata)>0:  for x in range(len(self.newdata['MAIN'])):    threading.Thread(target=self.download_main,args=(self.newdata['MAIN'][x],x)).start()    for x in self.newdata['COLOR']:    threading.Thread(target=self.download_color,args=(x,)).start()  for x in range(len(self.newdata['DETAIL'])):    threading.Thread(target=self.download_detail,args=(self.newdata['DETAIL'][x],x)).start() return def download_main(self,url,index): try:  img = requests.get("http:"+url,stream=True,headers = self.headers,timeout=10) except:  print(sys.exc_info())  return if img.ok:  if not os.path.exists(self.name+"/main"):  try:   os.makedirs(self.name+"/main")  except:   pass  imgs = open(self.name+"/main/%s.jpg"%index,"wb")  imgs.write(img.content)  imgs.close()   def download_color(self,url):   try:  img = requests.get("http:"+url[list(url.keys())[0]][0],stream=True,headers = self.headers,timeout=10) except:  print(sys.exc_info())  return if img.ok:  if not os.path.exists(self.name+"/color"):  try:   os.makedirs(self.name+"/color")  except:   pass  if "/"in list(url.keys())[0]:  color = list(url.keys())[0].replace("/","_")  elif "//" in list(url.keys())[0]:  color = list(url.keys())[0].replace("//","_")  else:  color = list(url.keys())[0]  imgs = open(self.name+"/color/%s.jpg"%color,"wb")  imgs.write(img.content)  imgs.close() def download_detail(self,url,index): try:  img = requests.get(url,stream=True,headers = self.headers,timeout=10) except:  print(sys.exc_info())  return if img.ok:  if not os.path.exists(self.name+"/detail"):  try:   os.makedirs(self.name+"/detail")  except:   pass    imgs = open(self.name+"/detail/%s.jpg"%index,"wb")  imgs.write(img.content)  imgs.close()  if __name__ =="__main__":  sid = 528766269341 ## 這里輸入天貓寶貝ID taobao = spider(sid,"下載圖片/T") taobao.matchs() taobao.download()

以上這篇python 爬蟲一鍵爬取 淘寶天貓寶貝頁面主圖顏色圖和詳情圖的教程就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嘉鱼县| 苏尼特左旗| 江津市| 襄垣县| 聊城市| 麻江县| 固始县| 苏州市| 呼玛县| 施秉县| 麻栗坡县| 临武县| 永善县| 明光市| 钟祥市| 湖南省| 三门峡市| 伊通| 宝鸡市| 旌德县| 通州市| 伽师县| 蒲江县| 开平市| 柞水县| 芷江| 高碑店市| 昭平县| 乌兰县| 长武县| 博白县| 温泉县| 平安县| 奎屯市| 洛扎县| 磴口县| 洛浦县| 于都县| 商南县| 松潘县| 庆城县|