from selenium import webdriver
import time
from bs4 import BeautifulSoup class douyuSelenium():
#初始化,启动斗鱼浏览器
def setUp(self):
self.driver = webdriver.PhantomJS() #获取斗鱼房间信息
def testDouyu(self):#'https://www.douyu.com/directory/all'
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'})
nums = soup.find_all('span',{'class':'dy-num fr'})
for title,num in zip(titles,nums):
info = "房间标题:" + title.get_text().strip() + '\t' + "人气:" + num.get_text().strip()
print(info) #下一页
#查找下一页 在最后一页会有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()

selenium,webdriver爬取斗鱼主播信息 实操的更多相关文章

  1. 『Scrapy』爬取斗鱼主播头像

    分析目标 爬取的是斗鱼主播头像,示范使用的URL似乎是个移动接口(下文有提到),理由是网页主页属于动态页面,爬取难度陡升,当然爬取斗鱼主播头像这么恶趣味的事也不是我的兴趣...... 目标URL如下, ...

  2. 爬虫之selenium爬取斗鱼主播图片

    这是我GitHub上简单的selenium介绍与简单使用:https://github.com/bwyt/spider/tree/master/selenium%E5%9F%BA%E7%A1%80 & ...

  3. 【爬虫小程序:爬取斗鱼所有房间信息】Xpath

    # 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正from selenium import webdriver import time class Douyu: "&q ...

  4. [python爬虫] Selenium定向爬取PubMed生物医学摘要信息

    本文主要是自己的在线代码笔记.在生物医学本体Ontology构建过程中,我使用Selenium定向爬取生物医学PubMed数据库的内容.        PubMed是一个免费的搜寻引擎,提供生物医学方 ...

  5. 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(多进程版)

    # 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正 import requests from lxml import etree from multiprocessing imp ...

  6. 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(协程池版)

    # 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正 from gevent import monkey monkey.patch_all() from gevent.pool i ...

  7. 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(线程池版)

    # 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正 from queue import Queue import requests from lxml import etree ...

  8. 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(多线程版)

    # 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正 from queue import Queue import requests from lxml import etree ...

  9. python实战之原生爬虫(爬取熊猫主播排行榜)

    """ this is a module,多行注释 """ import re from urllib import request # B ...

随机推荐

  1. LeetCode #237. Delete Node in a Linked List 删除链表中的节点

    https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 非常巧妙的一道题. 题目没有给head,心想没有head我怎么才能找到要删 ...

  2. [HDU 1529]Cashier Employment(差分约束系统)

    [HDU 1529]Cashier Employment(差分约束系统) 题面 有一个超市,在24小时对员工都有一定需求量,表示为\(r_i\),意思为在i这个时间至少要有i个员工,现在有n个员工来应 ...

  3. Anaconda Jupyter WinError2:The system cannot find the file specified

    Traceback (most recent call last): File "C:\Users\builder\Miniconda3\Scripts\conda-build-script ...

  4. 认识Mybatis

    什么是Mybatis? MyBatis 是一款优秀的基于Java的持久层框架(来源于“internet”和“abatis”的组合),它支持定制化 SQL.存储过程以及高级映射. MyBatis 避免了 ...

  5. windows与linux安装Python虚拟环境

    我这里觉得还是一步到位用virtualenvwrapper  工具,不再讲述virtualenv了,有了工具很好用 windows : 首先安装工具 pip install virtualenvwra ...

  6. VPX板卡 基于XC7K325T的3U VPX FMC接口数据收发预处理平台

    一.板卡概述       标准VPX 3U板卡, 基于Xilinx公司的FPGAXC7K325T-2FFG900 芯片,pin_to_pin兼容FPGAXC7K410T-2FFG900 ,支持PCIe ...

  7. 微信小程序(13)--页面滚动到某个位置添加类效果

    微信小程序页面滚动到某个位置添加类,盒子置顶效果. <!-- vh,是指CSS中相对长度单位,表示相对视口高度(Viewport Height),1vh = % * 视口高度 --> &l ...

  8. vue,一路走来(1)--构建vue项目

    2016年12月--2017年5月,接触前端框架vue,一路走来,觉得有必要把遇到的问题记录下来. 那时,vux用的是1.0的vue,然而vue2.0已经出来了,于是我结合了mint-ui一起来做项目 ...

  9. Sass函数:random()函数

    random() 函数是用来获取一个随机数 >> random() 0.03886 >> random() 0.66527 >> random() 0.8125 & ...

  10. HTML基础iframe 实现聚合浏览器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...