1.安裝,在命令行輸入:pip install requests-html,安裝成功后,在Pycharm引入即可。

2.代碼如下所示:
from requests_html import HTMLSession
import requests
session = HTMLSession()
r = session.get('http://www.win4000.com/wallpaper_2358_0_10_1.html')
images = r.html.find('ul.clearfix > li > a') #獲取到網頁上所有a標簽url
def save_Image(url,title): #定義一個函數,用於保存圖片到指定目錄下(E盤下需手動新建bg文件夾)
html_response = requests.get(url)
with open('E:/bg/'+title+'.jpg','wb') as file:
file.write(html_response.content)
#查找頁面中背景圖,找到鏈接,訪問查看大圖,並獲取大圖地址
for image in images:
image_url = image.attrs['href'] #獲取到每張圖片屬性值為href的url
if '/wallpaper_detail' in image_url:
r = session.get(image_url)
item_url = r.html.find('img.pic-large',first=True) #獲取到href下的src的url
url = item_url.attrs['src']
title = item_url.attrs['title']
print(url+title)
save_Image(url,title)
3.在指定目錄即可查看到爬下來的圖片

