zoukankan      html  css  js  c++  java
  • 【python】虎牙直播爬虫项目

    虎牙直播爬虫项目:

    #导入selenium相关的工具
    from selenium import webdriver
    from lxml import etree
    class Huya(object):
        #初始化
        def __init__ (self):
            #通过浏览器加载网页
            self.driver = webdriver.PhantomJS()
            #要统计的数据
            self.room_count = 0 #房间数量
            self.audience_count = 0 #房间观众数量
            
            
        #执行爬虫
        def run(self):
            #打开网页
            self.driver.get('https://www.huya.com/l')
            
            #爬取相关的数据
            content = etree.HTML(self.driver.page_source) #获取并解析网页的源码
            #获取房间信息
            rooms = content.xpath('.//ul[@id="js-live-list"]/li')
            for room in rooms:
                #获取房间名称
                roomname = ''
                tmp = room.xpath('./a[contains(@class,"title")]/text()')
                if len(tmp) > 0:
                    roomname = tmp[0]
                
                #获取房间人气
                audience = ''
                tmp = room.xpath('//span[@class="num"]/i[@class="js-num"]/text()')
                if len(tmp) > 0:
                    audience = tmp[0]
                    
                print('房间观众:%s,房间名称: %s' % (audience, roomname))
                
                #增加房间数
                self.room_count += 1
                #增加观众数
                if audience[-1] == '万':
                    audience = audience[0:-1] #2.2万 -- 2.2
                    audience = int(float(audience) * 10000)
                    self.audience_count += audience
                else:
                    self.audience_count += int(audience)
            
            #输出结果
            print('当前直播间数量: %d' % self.room_count)
            print('当前观众数量: %d' % self.audience_count)
            
        def test(self):
            #打开网页
            self.driver.get('https://www.huya.com/l')
            
            #循环遍历每一页
            page = 0
            while True:
                import time
                time.sleep(2)
                
                page += 1
                #查找class
                ret = self.driver.page_source.find('laypage_next')
                if ret > 0:
                    print('第%d页' % page)
                else:
                    print('最后一页')
                    break
                #点击下一页按钮
                self.driver.find_element_by_class_name('laypage_next').click()
            
    if __name__ == '__main__':
        huya = Huya()
        huya.run()
        #huya.test()

    运行结果:(截取部分)

    房间观众:19.7万,房间名称: 湖北声优第一仙女中单
    房间观众:48.2万,房间名称: v:带粉无敌~!
    房间观众:48.1万,房间名称: G神~世界第一狙
    房间观众:38.2万,房间名称: 今晚8:00才艺大比拼/招优质主持
    房间观众:23.6万,房间名称: 第861集,简单唱歌
    房间观众:19.3万,房间名称: 单排锐雯峡谷之巅今天宗师崽
    房间观众:47.9万,房间名称: 30人生化千分下单上号乱秀
    房间观众:47.1万,房间名称: 宇宙级吕布单排轻松101
    房间观众:37.5万,房间名称: 派活13-公爵王Y33
    房间观众:22.7万,房间名称: 是你的心动女孩嘛~
    房间观众:16.9万,房间名称: 南通王者妹妹 电一~ 一起玩
    当前直播间数量: 120
    当前观众数量: 96177000
  • 相关阅读:
    SVN如何新建用户并分配权限
    Android性能测试工具Emmagee介绍
    Jquery 中的$(this) 和javascript中的this
    Django 的开始之路
    Python 100道题深入理解
    怎么使用jquery判断一个元素是否含有一个指定的类(class)
    windows下面安装Python和pip
    Waves:类Material Design 的圆形波浪(涟漪)点击特效插件
    Python 基础语法(三)
    Python 基础语法(四)
  • 原文地址:https://www.cnblogs.com/helenlee01/p/12617439.html
Copyright © 2011-2022 走看看