#coding=gbk
import urllib.request
import re
import random
#建议ua代理池
keyword = "休闲裤"
key = urllib.request.quote(keyword)
uapools = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.33 Safari/534.3 SE 2.X MetaSr 1.0",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1 QQBrowser/6.9.11079.201",
]
#建议使用UA代理池的函数
def ua(uapools) :
thisua = random.choice(uapools) #随机选取UA代理池的UA元素
headers = ("User-Agent",thisua) #设置报头信息
opener = urllib.request.build_opener() #定义opener变量
opener.addheaders = [headers] #设置报头
urllib.request.install_opener(opener) #将opener设置成全局变量
for i in range (1,10):
url = "https://s.taobao.com/search?q="+key+"&s="+str((i-1)*44)
print(url)
ua(uapools)
data = urllib.request.urlopen(url).read().decode("utf-8","ignore")
print(data)
pat='"pic_url":"//(.*?)"'
imglist = re.compile(pat).findall(data)
print(imglist)
for j in range(0,len(imglist)):
thispictrue = imglist[j]
keythispitrue = "http://"+ thispictrue
fh = "C:\\软件\\pictrue\\"+str(i)+str(j)+".jpg"
urllib.request.urlretrieve(keythispitrue,filename=fh)