淘宝只有简单的反爬,就是cookie反爬,加入了cookie即可。下面是代码,用的Beautifulsoup和正则做的,如果不懂可以留言
在这里插入代码片
import requests
from bs4 import BeautifulSoup
import urllib.request
import re
import os
import time
headers={
'cookie' : ‘写自己的账号的cookie',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
paqucishu=0
def get_one_page(url):
url.encode("UTF-8")
urls=requests.get(url,headers=headers)
urls=urls.text.encode("UTF-8")
# print(url)
# print(urls)
soup=BeautifulSoup(urls,"lxml")
# print(soup)
zhengze='"pic_url":"//(.*?)",'
zhengze_mingzi='"raw_title":"(.*?)",'
imgs=re.compile(zhengze).findall(str(soup))
mingzi=re.compile(zhengze_mingzi).findall(str(soup))
# imgs=soup.select("div > div.pic")
# print(imgs)
# print(imgs)
global paqucishu
for img,mingzi in zip(imgs,mingzi):
img="https://"+img
print(img)
try:
time.sleep(0.2)
file="E:/淘宝图抓取/超级短裙/"+str(mingzi)+".jpg"
urllib.request.urlretrieve(img,filename=file)
print("爬取"+str(paqucishu)+"成功")
paqucishu=int(paqucishu)+1
except Exception as result:
print('图片下载失败' + str(result))
def get_one_pages():
key=input("请输入你想爬取的图片的名称呼种类")
nums=int(input("请输入你想爬取的页数"))
for i in range(0,nums):
key_name=urllib.request.quote(key)
url="https://s.taobao.com/search?q="+key_name+"&s="+str(int(i)*44)
# print(url)
print("开启第")
print(i)
print("页爬取")
time.sleep(1)
get_one_page(url)
if __name__ == '__main__':
# print(os.getcwd())
get_one_pages()
#来自csdn
#the 程序员
#来自csdn
#the 程序员