Python3-selenium\phantomjs\bs4爬取斗鱼页面

40 篇文章 0 订阅
10 篇文章 0 订阅
from selenium import webdriver
import time
from bs4 import BeautifulSoup

class douyuSelenium():
    #初始化,启动斗鱼浏览器
    def setup(self):
        self.driver=webdriver.PhantomJS()

    #获取斗鱼房间信息
    def testDouyu(self):
        self.driver.get('https://www.douyu.com/directory/all')

        while True:
            time.sleep(2)

            #指定解析器,生成一个soup对象
            soup=BeautifulSoup(self.driver.page_source,'lxml')

            # 获取当前页面所有的房间标题,观众人数
            titles=soup.find_all('h3',{'class':'ellipsis'})
            # for title in titles:
            #     title=title.text.strip()
            #     print(title)
            # #人气
            nums=soup.find_all('span',{'class':'dy-num fr'})
            # for num in nums:
            #     num=num.text.strip()'房间标题:'+title.text.strip()+'\t'+'人气:'+num.text
            #     print(num)
            # print(title+'\t'+num)
            for title,num in zip(titles,nums):#感觉标题和人气不匹配
                info='房间标题:' + title.text.strip() + '\t' + '人气:' + num.text
                print(info)

            #下一页
            #查找下一页
            # self.driver.find_element_by_class_name('shark-pager-next shark-pager-disable shark-pager-disable-next')
            if self.driver.page_source.find('shark-pager-disable-next')!=-1:
                break
            #点击
            next_page=self.driver.find_element_by_class_name('shark-pager-next')
            next_page.click()
    #退出
    def shutdown(self):
        print('加载完成。。。。')
        self.driver.quit()
if __name__=='__main__':
    douyu=douyuSelenium()
    douyu.setup()
    douyu.testDouyu()
    douyu.shutdown()
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值