爬虫(七):虎牙直播爬虫

由于一直闲赋在家,很烦躁。人生路差点走歪,所幸还是回来了。

一、环境

平台:windows10 解释器:vscode

二、网页分析

这里主要有三个地方:
(1)周榜是json动态生成的
在这里插入图片描述

"https://www.huya.com/cache5min.php?m=WeekRank&do=getItemsByPid&pid=18405890
只有pid这个数据是不一样的当然也是能在网页源码中发现

在这里插入图片描述

这个是贵宾榜单 也找到了,不过没有拿
https://www.huya.com/cache1min.php?m=VipBarList&tid=184005893&sid=184005893

在这里插入图片描述

这是粉丝榜单,也没有取
https://www.huya.com/cache.php?m=Fans&do=getFansScoreUpList&profileUid=184005893

在这里插入图片描述

这是工会信息也没有取
https://chgate.huya.com/proxy/index?service=thrift_sign&iface=getSignChannelInfo&callback=getSignChannelInfo&data=184005893

(2) 主播直播最近动态
"https://liveapi.huya.com/moment/getMomentListByUidForWeb?&pid="184005893
直接给网址所有的都只有pid不一样

三、代码

import scrapy
from copy import deepcopy
import re
import json

class HySpider(scrapy.Spider):
    name = 'hy'
    allowed_domains = ['huya.com']
    start_urls = ['http://huya.com/1']

    def parse(self, response):
        print("111"*20)
        a_list=response.xpath('//div[@class="m-bd"]//a')
        for a in a_list:
            item={}
            item["href"]=a.xpath('./@href').extract_first()
            item["b_cate"]=a.xpath('./span/text()').extract_first()
            yield scrapy.Request(
                item["href"],
                callback=self.cate_data_parse,
                meta={"item":deepcopy(item)}
            )
        
    def cate_data_parse(self,response):
        item=response.meta["item"]
        li_list=response.xpath('//ul/li[@class="game-live-item"]')
        for li in li_list:
            item["name"]=li.xpath('./span/span[1]/img/@alt').extract_first()
            item["title"]=li.xpath('./a[2]/@title').extract_first()
            item["Popularity"]=li.xpath("./span/span/i[2]/text()").extract_first()
            item["pid"]=li.xpath("./@data-lp").extract_first()
            item['img']=li.xpath("./span/span/img/@src").extract_first()
            item["room_href"]=li.xpath("./a[2]/@href").extract_first()
            yield scrapy.Request(
                item['room_href'],
                callback=self.room_data,
                meta={"item":deepcopy(item)}
            )

    def room_data(self,response):
        item=response.meta["item"]
        item["room_num"]=response.xpath('//div/span[@class="host-rid"]/em/text()').extract_first()
        weekend_href="https://www.huya.com/cache5min.php?m=WeekRank&do=getItemsByPid&pid="+item["pid"]
        yield scrapy.Request(
            weekend_href,
            callback=self.weekend_data,
            meta={"item":deepcopy(item)}
        )

    def weekend_data(self,response):
        item=response.meta["item"]
        #https://www.huya.com/cache5min.php?m=WeekRank&do=getItemsByPid&pid=78849323
        #https://www.huya.com/cache5min.php?m=WeekRank&do=getItemsByPid&pid=17363578
        html=response.text
        name= re.findall('''"sNickName":(.*?),"iScore":(.*?)"''',html)
        item["week_income"]=name
        dynamic_href="https://liveapi.huya.com/moment/getMomentListByUidForWeb?&pid="+item["pid"]
        yield scrapy.Request(
            dynamic_href,
            callback=self.dynamic,
            meta={"item":deepcopy(item)}
        )

    def dynamic(self,response):
        item=response.meta["item"]
        html=response.text
        video= re.findall('''"title":"(.*?)"''',html)
        item["dynamic"]=video
        print(item)
        yield item

四、运行效果图

在这里插入图片描述

五、不足

周贡献榜单的数据在原网页中是unicode编码的、使用re匹配下的数据无法进行解码,就很烦。暂时待解决

数据库以及redis去重很简单也就没有做

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值