python爬取虎牙直播顏值區美女主播照片
阿新 • • 發佈:2020-10-07
前言
本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯絡我們以作處理。
PS:如有需要Python學習資料的小夥伴可以加點選下方連結自行獲取
基本環境配置
- python 3.6
- pycharm
- requests
- parsel
相關模組pip安裝即可
確定網址
https://www.huya.com/g/2168
請求網頁
import requests 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) print(repsonse.text)
解析網頁資料
import parsel 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)
實現效果