python爬取虎牙直播颜值区美女主播照片

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取

python免费学习资料以及群交流解答点击即可加入


基本环境配置

  • python 3.6
  • pycharm
  • requests
  • parsel

相关模块pip安装即可

确定网址

https://www.huya.com/g/2168


python爬取虎牙直播颜值区美女主播照片_第1张图片

请求网页

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)

实现效果

python爬取虎牙直播颜值区美女主播照片_第2张图片
python爬取虎牙直播颜值区美女主播照片_第3张图片
python爬取虎牙直播颜值区美女主播照片_第4张图片
python爬取虎牙直播颜值区美女主播照片_第5张图片
python爬取虎牙直播颜值区美女主播照片_第6张图片
python爬取虎牙直播颜值区美女主播照片_第7张图片

你可能感兴趣的:(Python爬虫,python,数据挖掘,爬虫)