网上的淘宝爬取图片的代码一般都已经不能实际运行了,在查看淘宝网源代码是找不到图片源地址,估计采取了反爬技术。
又去京东看了下,发现很容易爬取。
根据下面网址构建url
https://list.jd.com/list.html?cat=670%2C671%2C1105&go=0
https://list.jd.com/list.html?cat=670,671,1105&page=2&sort=sort_totalsales15_desc&trans=1&JL=6_0_0#J_main
再根据如下构建正则表达式
https://img13.360buyimg.com/n7/jfs/t1/41579/36/1094/201211/5cc51607E2e6dcf1b/147afe82fc5af7e8.jpg
https://img10.360buyimg.com/n7/jfs/t1/55849/3/7222/209576/5d4d0ea7Ea7e65dc1/13ab0dacd9200a17.jpg
https://img14.360buyimg.com/n7/jfs/t1/57308/8/9091/158396/5d68c035E71bcd899/264007e08924f401.jpg
附下效果图:
import urllib.request
import re
import random
keyname="游戏本"
key=urllib.request.quote(keyname)
uapools = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393",
]
def ua(uapools):
thisua=random.choice(uapools)
print(thisua)
headers = ("User-Agent", thisua)
opener = urllib.request.build_opener()
opener.addheaders = [headers]
urllib.request.install_opener(opener)
for i in range(1,11):
url = "https://list.jd.com/list.html?cat=670,671,1105&page="+str(i)
ua(uapools)
data=urllib.request.urlopen(url).read().decode("utf-8","ignore")
print("1")
pat='data-lazy-img="//(.*?)"'
imglist=re.compile(pat).findall(data)
for j in range(0, len(imglist)):
thisimg=imglist[j]
thisimgurl="http://"+thisimg
localfile="C:/Users/abc/Desktop/data/"+str(i)+str(j)+".jpg"
urllib.request.urlretrieve(thisimgurl,filename=localfile)