• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python 爬虫一键爬取 淘宝天猫宝贝页面主图颜色图和详情图的教程

python 搞代码 4年前 (2022-01-07) 15次浏览 已收录 0个评论

今天小编就为大家分享一篇python 爬虫一键爬取 淘宝天猫宝贝页面主图颜色图和详情图的教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

实例如下所示:

 import requests import re,sys,os import json import threading import pprint class 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(ps<strong style="color:transparent">来源gaodaima#com搞(代@码网</strong>v) 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-600"%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-600"%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-600"%index,"wb") imgs.write(img.content) imgs.close() if __name__ =="__main__": sid = 528766269341 ## 这里输入天猫宝贝ID taobao = spider(sid,"下载图片/T") taobao.matchs() taobao.download() 

以上这篇python 爬虫一键爬取 淘宝天猫宝贝页面主图颜色图和详情图的教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网

以上就是python 爬虫一键爬取 淘宝天猫宝贝页面主图颜色图和详情图的教程的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python 爬虫一键爬取 淘宝天猫宝贝页面主图颜色图和详情图的教程
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址