标签:lxml 遍历 request col https mod pen time 爬虫
import requests from bs4 import BeautifulSoup import time import re t = 0 #用于给图片命名 for i in range(10): url = "https://list.jd.com/list.html?cat=9987,653,655&page=%s&sort=sort_rank_asc&trans=1&JL=6_0_0&ms=6#J_main"%i #起始url #设置header,模拟浏览器发出请求 header = { ‘user-agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36‘, } data = requests.get(url,headers=header).content soup = BeautifulSoup(data,‘lxml‘)#使用beautifulsoup解析上面获得的html为文档 img_link = soup.select(‘div#plist div.p-img‘)#先缩小目标数据所在的范围 link_list = re.findall(‘//img[0-9|\.|a-z|\/|A-Z]+‘,str(img_link))#在上面的范围中使用正则找到对应的img src #遍历img src 下载到本地 for url in link_list: url = ‘https:‘+str(url) data = requests.get(url).content filename = "C:/Users/Administrator/Desktop/京东图片/" + str(t) +".jpg" t += 1 with open(filename,mode=‘wb‘) as f: f.write(data)
标签:lxml 遍历 request col https mod pen time 爬虫
原文地址:https://www.cnblogs.com/xiaohuyi/p/10347039.html