前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理,
本次目標
爬取娟娟壁紙網的圖片
受難者地址
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)
運行結果
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/207117.html
標籤:其他
