Python——selenium爬取斗鱼房间信息
from selenium import webdriver
import os
import json
import time class Douyu:
def __init__(self):
# 1.发送首页的请求
self.driver = webdriver.Chrome('../chromedriver.exe')
self.driver.get('https://www.douyu.com/g_yz') # 获取页面内容
def get_content(self):
time.sleep(3)
li_list = self.driver.find_elements_by_xpath("//*[@class='layout-Cover-list']/li[@class='layout-Cover-item']")
length = len(li_list)
contents = [] # 遍历房间列表
for i in range(length):
item = {}
item['主播'] = self.driver.find_elements_by_xpath('//h2[@class="DyListCover-user"]')[i].text
item['房间名'] = self.driver.find_elements_by_xpath('//h3[@class="DyListCover-intro"]')[i].get_attribute(
'title')
item['热度'] = self.driver.find_elements_by_xpath('//span[@class="DyListCover-hot"]')[i].text
item['封面'] = self.driver.find_elements_by_class_name('DyImg-content')[i].get_attribute(
'src')
contents.append(item)
return contents # 保存数据到本地
def save_content(self, contents):
with open('douyu.json', 'a',encoding='utf-8') as f:
for content in contents:
json.dump(content, f, ensure_ascii=False, indent=2)
f.write(os.linesep) def run(self):
# 1.发送首页的请求:初始化时已经发送请求
# 2.获取第一页的数据
contents = self.get_content()
self.save_content(contents) # 3.循环:点击下一页按钮,只要没有下一页的按钮
while self.driver.find_elements_by_class_name('dy-Pagination-item-custom')[1]:
# 点击下一页的按钮
self.driver.find_elements_by_class_name('dy-Pagination-item-custom')[1].click()
# 4.继续获取下一页的内容
contents = self.get_content()
# 4.保存下一页保存内容
self.save_content(contents) if __name__ == '__main__':
douyu = Douyu()
douyu.run()
Python——selenium爬取斗鱼房间信息的更多相关文章
- Python+Selenium爬取动态加载页面(2)
注: 上一篇<Python+Selenium爬取动态加载页面(1)>讲了基本地如何获取动态页面的数据,这里再讲一个稍微复杂一点的数据获取全国水雨情网.数据的获取过程跟人手动获取过程类似,所 ...
- Python+Selenium爬取动态加载页面(1)
注: 最近有一小任务,需要收集水质和水雨信息,找了两个网站:国家地表水水质自动监测实时数据发布系统和全国水雨情网.由于这两个网站的数据都是动态加载出来的,所以我用了Selenium来完成我的数据获取. ...
- selenium模块使用详解、打码平台使用、xpath使用、使用selenium爬取京东商品信息、scrapy框架介绍与安装
今日内容概要 selenium的使用 打码平台使用 xpath使用 爬取京东商品信息 scrapy 介绍和安装 内容详细 1.selenium模块的使用 # 之前咱们学requests,可以发送htt ...
- Python+selenium爬取智联招聘的职位信息
整个爬虫是基于selenium和Python来运行的,运行需要的包 mysql,matplotlib,selenium 需要安装selenium火狐浏览器驱动,百度的搜寻. 整个爬虫是模块化组织的,不 ...
- 利用selenium爬取京东商品信息存放到mongodb
利用selenium爬取京东商城的商品信息思路: 1.首先进入京东的搜索页面,分析搜索页面信息可以得到路由结构 2.根据页面信息可以看到京东在搜索页面使用了懒加载,所以为了解决这个问题,使用递归.等待 ...
- Scrapy实战篇(七)之Scrapy配合Selenium爬取京东商城信息(下)
之前我们使用了selenium加Firefox作为下载中间件来实现爬取京东的商品信息.但是在大规模的爬取的时候,Firefox消耗资源比较多,因此我们希望换一种资源消耗更小的方法来爬取相关的信息. 下 ...
- 简单的python爬虫--爬取Taobao淘女郎信息
最近在学Python的爬虫,顺便就练习了一下爬取淘宝上的淘女郎信息:手法简单,由于淘宝网站本上做了很多的防爬措施,应此效果不太好! 爬虫的入口:https://mm.taobao.com/json/r ...
- 爬虫—Selenium爬取JD商品信息
一,抓取分析 本次目标是爬取京东商品信息,包括商品的图片,名称,价格,评价人数,店铺名称.抓取入口就是京东的搜索页面,这个链接可以通过直接构造参数访问https://search.jd.com/Sea ...
- python itchat 爬取微信好友信息
原文链接:https://mp.weixin.qq.com/s/4EXgR4GkriTnAzVxluJxmg 「itchat」一个开源的微信个人接口,今天我们就用itchat爬取微信好友信息,无图言虚 ...
随机推荐
- linux信号量使用
#include <pthread.h> #include <semaphore.h> #include <unistd.h> #include <stdio ...
- JAVA中跨平台分隔符
在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常. 比如说要在temp目录下建立一个te ...
- requirejs——define——普通模块
一.普通模块可能包含的内容: 一个模块对应着一个js文件,由于模块可能包含着以下三种内容:模块名.依赖模块.返回给其他模块使用的参数:因此js文件根据包含内容的不同而写法不同. 一.传统的js脚本文件 ...
- Delphi IOS 上架
http://docwiki.embarcadero.com/RADStudio/Seattle/en/IOS_Mobile_Application_Development http://docwik ...
- arm-linux-gcc-4.5.1的安装…
原文地址:arm-linux-gcc-4.5.1的安装方法作者:游牧 说明:我使用的是在虚拟机下的红帽linux5 ,通过终端工具SecureCRT操作,使用虚拟机等其他工具的过程大致相同 1.使用r ...
- 修改eclipse默认workspace
三种方法 (只改其一可能无效,最好都试试) 1. 修改exlipse安装目录下\configuration\.settings\org.eclipse.ui.ide.prefs文件,修改RECENT_ ...
- 用插件NPOI读写excel
1.用插件NPOIusing NPOI.SS.UserModel;using NPOI.XSSF.UserModel;using NPOI.HSSF.UserModel; public class E ...
- dataview 组件使用示例
来自<sencha touch 权威指南> ------------------------------- 例子1——app.js代码如下: Ext.require(['Ext.data. ...
- Luogu 3616 富金森林公园
刚看到此题的时候:sb分块??? Rorshach dalao甩手一句看题 于是回去看题……果然是题读错了…… [思路] 对权值离散化后(要先读入所有输入里的权值一起离散化……所以一共有4e4个数据( ...
- Visual Studio 2010调试本地 IIS 站点
点击vs的Debug-Attach to Process选中 w3wp.exe,然后点击Attach, vs便进入debug模式.