Python如何实现爬取美女主播图片
这篇文章将为大家详细讲解有关Python如何实现爬取美女主播图片,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
前言
本文的文字及图片来源于网络,仅供学习、交流使用
项目目标
主播照片
目标地址
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)
关于“Python如何实现爬取美女主播图片”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。