python实现淘宝商品批量下载详情页图片
目录
1.打开某件宝贝的详情页地址url
例如下图这款windows10鼠标垫宝贝,url地址为下图红色框框里!
注意:目前此代码不支持天猫详情页图片下载。可以关注我后续的文章,也可以自行研究,原来一样。只需用bs4库提取里面的图片地址即可!
2.在chrome浏览器查看自己的淘宝cookie值
打开chrome谷歌浏览器,按F11,选择红框的选项。复制红色框cookie的值
3.将url和cookie值替换到下面的代码中
废话不说,撸上代码
import re import requests import json from PIL import Image from io import BytesIO from bs4 import BeautifulSoup import os import requests import re # 填入自己的cookie值 headers = { 'Cookie':'thw=cn; hng=CN%7Czh-CN%7CCNY%7C156; miid=183661675555435285645; tracknick=fang510515; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0; cna=4sqnF8VfoEkCAXQXEWvoraFY; enc=sEsCaCgbG%2F7kOXEDpdvq%2Bj327K0mOB5VonNlrBkMgJkw4qAl6s%2FwfOjmXjo%2BTmwhSNGIv5A%2ByJc0sMuYvGrKLQ%3D%3D; t=cc2bbe7d83fd5b912599657b58845855; lgc=fang510515; cookie2=1d7792f371a1102sdffds5bdfce0599; _samesite_flag_=true; xlly_s=1; _m_h5_tk=f73a3b46fa69eeb62580bb4ecc79fad2_1617092244832; _m_h5_tk_enc=7e09360c3902979942a9bbc1fe929051; sgcookie=E100NOaY0%2FXDpKjXKEyIRdTwOhgIv6%2Bjd8EopuabwNQSkPfNNz7f%2BQU2wXBPsURcOxb36kPkQNL8tU0iniNsDjgLYw%3D%3D; unb=734830797; uc3=nk2=BdGi0ISfvR%2FaVg%3D%3D&lg2=U%2BGCWk%2F75gdr5Q%3D%3D&id2=VAKJAqbsk6Uh&vt3=F8dCuAtXiGqgXQCM45Q%3D; csg=64d10512; cookie17=VAKJAqbsk6Uh; dnk=fang510515; skt=5beef9053deb82cc; existShop=MTYxNzA4NDMzMw%3D%3D; uc4=id4=0%40VhvFlqHxePfviKXqsPggkrddrlo%3D&nk4=0%40B12%2BhJ%2Fdh7REbYJoaCvbYs281MFQ; publishItemObj=Ng%3D%3D; _cc_=Vq8l%2BKCLiw%3D%3D; _l_g_=Ug%3D%3D; sg=575; _nk_=fang510515; cookie1=U7Si1egRkbaxeC7%2Fe4EJGKbb9eDczzOuyawQwm8BZgY%3D; v=0; lLtC1_=1; mt=ci=14_1; _tb_token_=bpTggBixbw0F8; uc1=cookie14=UIHiLt3xD8xYTw%3D%3D&pas=0&cookie21=VFC%2FuZ9ajCbF8%2BYBpbBdiw%3D%3D&cookie16=Vq8l%2dfsasSLZMFWHxqs8fwqnEw%3D%3D&existShop=true&cookie12=Uoe1hdEVxsvQ1Q%3D%3D; tfstk=cNOfB-DHjoqfcIDagEgz_A5rKRfFZF4C5xsDlpONIXqbfgLfirFFAiHBIlFRpa1..; l=eBEyWHVRjzu6i_KLXOfwourza77OSIRAguPzaNbMiOCPOJC65q-RW6ZVZRYBC3GVh60wR3-2Y4KHBdfb4wsWRKe5DDwQHmn; isg=BJycLQZiEM2Xf-QS1pfG3WP4bbpOFUA_XbV3z3adqAdrwTxLniUQzxJzJSk5yXiX', 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac 05 X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36' } def downloadtaobaoUrl(url_taobao): headers={ "User-Agent" : "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) ", "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language" : "en-us", "Connection" : "keep-alive", "Accept-Charset" : "GB2312,utf-8;q=0.7,*;q=0.7" } r=requests.get(url_taobao,headers = headers) # r.encoding="UTF-8" data = r.text # 获取商品标题文字 bs = BeautifulSoup(data,'lxml') name=bs.h3.string # 获取详情页图片2 data = re.findall("auctionImages(.*),",data)[0] # print (data) p1 = re.findall("//(.*)",str(data))[0] p1 = p1.replace('\"','') p1 = p1.replace('//','') # print(p1) num = 0; new_list = p1.split(",") # print(new_list) for i in(new_list): num = num +1; i="https://"+i r=requests.get(i,headers = headers) image = Image.open(BytesIO(r.content)) # 保存当前目录上 image.save('./%s.jpg' % (num)) print("任务完成") if __name__ == '__main__': # 只需修改此处的淘宝宝贝详情页的地址url url_taobao = "https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-5520973149.25.3ebf5007ovPXIZ&id=641654834351" downloadtaobaoUrl(url_taobao)
送福利了!关注下方的公众号:“优派编程”,搜索关键词“下载”,即可获得软件app下载资源和python、java等编程学习资料~
更多课程和学习资料请登录“方包博客”———http://fang1688.cn
更多资源请关注公众号,回复关键词获取