前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理,
專案目標
主播照片
目標地址
https://www.huya.com/g/2168
基本環境配置
- python 3.6
- pycharm
- requests
- parsel
爬蟲代碼
匯入工具
import requests import parsel
決議網頁
爬取資料
url = 'https://www.huya.com/g/2168' 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)selector = parsel.Selector(response.text)urls = selector.css('.live-list .game-live-item a img::attr(data-original)').getall() titles = selector.css('.live-list .game-live-item a img::attr(title)').getall() info_data = zip(urls, titles)for i in info_data: img_url = i[0].split('?')[0] title = i[1] img_url_response = requests.get(url=img_url, headers=headers) path = 'D:\\python\\demo\\虎牙\\img\\' + title + '.jpg' with open(path, mode='wb') as f: f.write(img_url_response.content) print(title)
運行代碼,效果如下
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/99194.html
標籤:Python
