实例如下所示:

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(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 爬虫一键爬取 淘宝天猫宝贝页面主图颜色图和详情图的教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。


您可能感兴趣的文章:简单的抓取淘宝图片的Python爬虫python爬虫获取淘宝天猫商品详细参数python实现爬取千万淘宝商品的方法python爬虫爬取淘宝商品信息简单的Python抓taobao图片爬虫

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

分页:12
转载请注明
本文标题:python 爬虫一键爬取 淘宝天猫宝贝页面主图颜色图和详情图的教程
本站链接:https://www.codesec.net/view/576950.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(34)