發現一個很棒的壁紙網站,所以我們應該把它爬取下來
前言
本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯繫我們以作處理
最近又發現了一個很棒的壁紙網站
環境
- Python3.6
- pycharm
本次目標
爬取網站高清
網站地址
//wallhaven.cc/
爬蟲程式碼
導入工具
import requests import re
請求網站
url = '//wallhaven.cc/toplist?page={}'.format(page) headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36' } response = requests.get(url=url, headers=headers)
解析網站數據
for i in urls: response_2 = requests.get(url=i, headers=headers) img_url = re.findall('<img id="wallpaper" src="(.*?)"', response_2.text, re.S)[0] title = img_url.split('-')[-1] download(title, img_url)
保存數據
def download(title, url): path = '保存地址' + title response = requests.get(url=url) with open(path, mode='wb') as f: f.write(response.content)