Python——selenium爬取斗鱼房间信息

 1 from selenium import webdriver
 2 import os
 3 import json
 4 import time
 5
 6
 7 class Douyu:
 8     def __init__(self):
 9         # 1.发送首页的请求
10         self.driver = webdriver.Chrome(‘../chromedriver.exe‘)
11         self.driver.get(‘https://www.douyu.com/g_yz‘)
12
13     # 获取页面内容
14     def get_content(self):
15         time.sleep(3)
16         li_list = self.driver.find_elements_by_xpath("//*[@class=‘layout-Cover-list‘]/li[@class=‘layout-Cover-item‘]")
17         length = len(li_list)
18         contents = []
19
20         # 遍历房间列表
21         for i in range(length):
22             item = {}
23             item[‘主播‘] = self.driver.find_elements_by_xpath(‘//h2[@class="DyListCover-user"]‘)[i].text
24             item[‘房间名‘] = self.driver.find_elements_by_xpath(‘//h3[@class="DyListCover-intro"]‘)[i].get_attribute(
25                 ‘title‘)
26             item[‘热度‘] = self.driver.find_elements_by_xpath(‘//span[@class="DyListCover-hot"]‘)[i].text
27             item[‘封面‘] = self.driver.find_elements_by_class_name(‘DyImg-content‘)[i].get_attribute(
28                 ‘src‘)
29             contents.append(item)
30         return contents
31
32     # 保存数据到本地
33     def save_content(self, contents):
34         with open(‘douyu.json‘, ‘a‘,encoding=‘utf-8‘) as f:
35             for content in contents:
36                 json.dump(content, f, ensure_ascii=False, indent=2)
37                 f.write(os.linesep)
38
39     def run(self):
40         # 1.发送首页的请求:初始化时已经发送请求
41         # 2.获取第一页的数据
42         contents = self.get_content()
43         self.save_content(contents)
44
45         # 3.循环:点击下一页按钮,只要没有下一页的按钮
46         while self.driver.find_elements_by_class_name(‘dy-Pagination-item-custom‘)[1]:
47             # 点击下一页的按钮
48             self.driver.find_elements_by_class_name(‘dy-Pagination-item-custom‘)[1].click()
49             # 4.继续获取下一页的内容
50             contents = self.get_content()
51             # 4.保存下一页保存内容
52             self.save_content(contents)
53
54
55 if __name__ == ‘__main__‘:
56     douyu = Douyu()
57     douyu.run()

原文地址:https://www.cnblogs.com/crazyechoaoo/p/10682715.html

时间: 2024-08-28 14:02:09

Python——selenium爬取斗鱼房间信息的相关文章

简易python爬虫 - 爬取站长论坛信息

爬取目标: 收集网站帖子里发帖人用户名,发帖人ID;帖子的ID,发帖内容;网站title 提前需要准备的python库 pip3 install requests //用于获得网站的源码 pip3 install bs4 //解析遍历网站标签 pip3 install urllib //解析网站的url 首先导入包 import requestsfrom bs4 import BeautifulSoupfrom urllib.parse import parse_qs,urlparse impo

python+selenium爬取百度文库不能下载的word文档

有些时候我们需要用到百度文库的某些文章时,却发现需要会员才能下载,很难受,其实我们可以通过爬虫的方式来获取到我们所需要的文本. 工具:python3.7+selenium+任意一款编辑器 前期准备:可以正常使用的浏览器,这里推荐chrome,一个与浏览器同版本的驱动,这里提供一个下载驱动的链接https://chromedriver.storage.googleapis.com/77.0.3865.40/chromedriver_win32.zip 首先我们来看一下百度文库中这一篇文章https

python爬虫爬取网上药品信息并且存入数据库

我最近在学习python爬虫,然后正好碰上数据库课设,我就选了一个连锁药店的,所以就把网上的药品信息爬取了下来. 1,首先分析网页 2,我想要的是评论数比较多的,毕竟好东西大概是买的人多才好.然后你会发现它的url地址是有规律的里面的j1是指第一页,j2第二页,这样构建一个url_list. 1 url_list = 'https://www.111.com.cn/categories/953710-a0-b0-c31-d0-e0-f0-g0-h0-i0-j%s.html'#然后循环获取响应 2

python selenium爬取kuku漫画

在爬取这个网站之前,试过爬取其他网站的漫画,但是发现有很多反爬虫的限制,有的图片后面加了动态参数,每秒都会更新,所以前一秒爬取的图片链接到一下秒就会失效了,还有的是图片地址不变,但是访问次数频繁的话会返回403,终于找到一个没有限制的漫画网站,演示一下selenium爬虫 # -*- coding:utf-8 -*- # crawl kuku漫画 __author__='fengzhankui' from selenium import webdriver from selenium.webdr

selenium爬取斗鱼所有直播房间信息

还是分析一下大体的流程: 首先还是Chrome浏览器抓包分析元素,这是网址:https://www.douyu.com/directory/all 发现所有房间的信息都是保存在一个无序列表中的li中,所以我们可以先获取一个装有li的element对象的列表,然后在对每个element逐一操作 分析斗鱼的翻页,有一个下一页按钮,是个li,class="dy-Pagination-item-custom",但是当烦到最后一页的时候,class="dy-Pagination-dis

python — selenium爬取微博指数

---恢复内容开始--- 需要用到的工具:python(pymouse.selenium).chrome.webdriver 使用的webdriver一定要和chrome的版本相匹配,具体的对应关系可以参考以下博客:http://blog.csdn.net/goblinintree/article/details/47335563.为了避免这个问题,最好直接使用最新的chrome和最新的webdriver. 以下代码是PyMouse的源码 # -*- coding: iso-8859-1 -*-

python selenium爬取QQ空间方法

from selenium import webdriver import time # 打开浏览器 dr = webdriver.Chrome() # 打开某个网址 dr.get('https://user.qzone.qq.com/2330331400/main') # 切换框架 dr.switch_to_frame('login_frame') time.sleep(1) dr.find_element_by_link_text('帐号密码登录').click() # 输入账号 dr.fi

python+selenium爬取关键字搜索google图片

1 # -*- coding: utf-8 -*- 2 3 import json 4 import os 5 import time 6 from multiprocessing import Pool 7 import multiprocessing 8 import requests 9 from selenium import webdriver 10 11 12 def get_image_links(keyword, num_requested = 1000): 13 ""

[Python爬虫] Selenium爬取新浪微博移动端热点话题及评论 (下)

这篇文章主要讲述了使用python+selenium爬取新浪微博的热点话题和评论信息.其中使用该爬虫的缺点是效率极低,傻瓜式的爬虫,不能并行执行等,但是它的优点是采用分析DOM树结构分析网页源码并进行信息爬取,同时它可以通过浏览器进行爬取中间过程的演示及验证码的输入.这篇文章对爬虫的详细过程就不再论述了,主要是提供可运行的代码和运行截图即可.希望文章对你有所帮助吧~ 参考文章 [python爬虫] Selenium爬取新浪微博内容及用户信息 [Python爬虫] Selenium爬取新浪微博客户