桌布不嫌棄多,今天帶你爬取動漫桌布網站(福利哦)
阿新 • • 發佈:2020-11-16
前言
本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯絡我們以作處理。
( 想要學習Python?Python學習交流群:1039649593,滿足你的需求,資料都已經上傳群檔案流,可以自行下載!還有海量最新2020python學習資料。 )
本次目標
爬取娟娟桌布網的圖片
受難者地址
http://www.jj20.com/
環境
- Python3.6
- pycharm
爬蟲程式碼
匯入工具
import requests import parsel
請求頭
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' }
解析目標網站的資料
for page in range(1, 17): url = 'http://www.jj20.com/bz/ktmh/list_16_cc_14_{}.html'.format(page) response = requests.get(url=url, headers=headers) selector = parsel.Selector(response.text) lis= selector.css('body > div:nth-child(7) > ul li') for li in lis: page_url = 'http://www.jj20.com/' + li.css('a:nth-child(1)::attr(href)').get() title = li.css('a:nth-child(1) img::attr(alt)').get() get_img(page_url, title)
儲存資料
def download(img_url, img_title): path= '儲存地址' + title + '.jpg' response = requests.get(url=img_url, headers=headers) with open(path, mode='wb') as f: f.write(response.content) print(img_url, img_title)