python爬蟲之下載京東頁面圖片
阿新 • • 發佈:2019-02-01
pen 縮小 use window com link_list sel class 模擬瀏覽器
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)
python爬蟲之下載京東頁面圖片