#爬取京东商品图片的小测试
访问的页面
图片链接的位置
# -*- coding = utf - 8 -*-
#@Time : 2020/7/16 14:54
#@Author : sunzy
#@File : jd.py
import re
import requests
import time
import os
dir = "jdtp"
if not os.path.exists(dir):
os.mkdir(dir)
baseurl = "https://search.jd.com/Search?keyword=%E8%BF%9B%E5%8F%A3%E9%9B%B6%E9%A3%9F&enc=utf-8&wq=%E8%BF%9B%E5%8F%A3%E9%9B%B6%E9%A3%9F&pvid=ryxp9pui.nhltvu"
head = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Edg/83.0.478.61"
}
response = requests.get(url=baseurl, headers = head)
#print(response.text)
html = response.text
findlinks = '<img width="220" height="220" data-img="\d" src="(.*?)".*?>'
#<img width="220" height="220" data-img="1" src="//img10.360buyimg.com/n7/jfs/t22093/305/286523587/498951/2ddddbdf/5b30b30dNd6c7eca0.jpg" data-lazy-img="done" source-data-lazy-img="">
urls = re.findall(findlinks, html)
for url in urls:
time.sleep(1)
file_name = url.split('/')[-1]
res = requests.get("http:"+url,headers = head)
with open(dir+'/'+file_name, "wb") as f:
f.write(res.content)