python selenium爬取斗鱼所有直播房间信息过程详解
作者:springionic 时间:2023-11-19 07:00:11
还是分析一下大体的流程:
首先还是Chrome浏览器抓包分析元素,这是网址:https://www.douyu.com/directory/all
发现所有房间的信息都是保存在一个无序列表中的li中,所以我们可以先获取一个装有li的element对象的列表,然后在对每个element逐一操作
分析斗鱼的翻页,有一个下一页按钮,是个li,class="dy-Pagination-item-custom"
,但是当烦到最后一页的时候,class="dy-Pagination-disabled dy-Pagination-next"
,所以我们要想利用selenium模拟点击这个按钮,我们应该利用
get_elements_by_xpath()这个函数,这样到最后一页就获取不到了,就可以终止程序了。而用elements的原因是当到最后一页的时候获取不到的话,element会报错
然后还是通用的套路:发送请求获取响应、提取数据和下一页的元素、保存数据、点击下一页的元素循环......
遇到的两个坑:
需要用time.sleep()函数强制等待页面加载完再获取元素,否则报错,睡几秒就看你的网速了
xpath定位的时候,网页上有的类如这样:class=" abc"
或者class="abc "
,前面或后面有空格的,xpath处理的时候也必须有空格,否则获取不到
代码实现:
import time
from selenium import webdriver
class DouyuSpider(object):
def __init__(self):
self.start_rul = 'https://www.douyu.com/directory/all'
self.driver = webdriver.Chrome()
def get_content_list(self):
time.sleep(10) # 强制等待10秒,否则可能报错
li_list = self.driver.find_elements_by_xpath('//ul[@class="layout-Cover-list"]/li')
content_list = []
for li in li_list:
item = {}
item['room_img'] = li.find_element_by_xpath('.//img[@class="DyImg-content is-normal "]').get_attribute('src')
item['room_title'] = li.find_element_by_xpath('.//h3[@class="DyListCover-intro"]').text
item['root_category'] = li.find_element_by_xpath('.//span[@class="DyListCover-zone"]').text
item['author_name'] = li.find_element_by_class_name('DyListCover-user').text
item['watch_num'] = li.find_element_by_class_name('DyListCover-hot').text
content_list.append(item)
print(item) # 打印每次获取到的直播房间的信息
# 获取下一页的元素,为了防止没有报错,这里用elements,翻到最后一页一定就没有了,返回一个列表
next_url = self.driver.find_elements_by_xpath('//li[@class=" dy-Pagination-next"]')
next_url = next_url[0] if len(next_url) > 0 else None
return content_list, next_url
def save_content_list(self, content_list):
pass # 保存数据这里就不再做演示
def run(self): # 实现主要逻辑
# 1.start_url
# 2.发送请求,获取响应
self.driver.maximize_window()
self.driver.get(self.start_rul)
# 3.提取数据,提取下一页的元素
content_list, next_url = self.get_content_list()
# 4.保存数据
self.save_content_list(content_list)
# 4.点击下一页元素,循环
while next_url is not None:
next_url.click()
content_list, next_url = self.get_content_list()
self.save_content_list(content_list)
if __name__ == '__main__':
douban = DouyuSpider()
douban.run()
来源:https://www.cnblogs.com/springionic/p/11140982.html
标签:python,selenium,爬取,斗鱼,房间,信息
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python列表删除的三种方法代码分享
2022-10-26 19:13:35
Python中使用gflags实例及原理解析
2023-01-28 20:25:24
MySQL如何查看正在运行的SQL详解
2024-01-22 22:48:48
简单的命令查看安装的python版本号
2022-03-12 17:23:06
![](https://img.aspxhome.com/file/2023/8/88838_0s.png)
mysql密码过期导致连接不上mysql
2024-01-22 12:28:06
分析python切片原理和方法
2021-04-19 21:39:03
![](https://img.aspxhome.com/file/2023/7/110047_0s.png)
Python3多进程 multiprocessing 模块实例详解
2022-05-27 11:11:51
Python matplotlib如何简单绘制不同类型的表格
2021-10-16 12:23:48
![](https://img.aspxhome.com/file/2023/7/75887_0s.png)
sql2008 还原数据库解决方案
2024-01-26 07:16:59
golang之数据验证validator的实现
2024-04-26 17:36:03
在Python中使用cookielib和urllib2配合PyQuery抓取网页信息
2023-10-12 14:15:19
![](https://img.aspxhome.com/file/2023/1/79741_0s.png)
django序列化时使用外键的真实值操作
2022-07-19 10:09:30
Python+Tkinter制作在线个性签名工具
2023-12-25 15:21:23
![](https://img.aspxhome.com/file/2023/3/123793_0s.png)
Mini,又个 Javascript 选择器
2009-10-06 14:48:00
![](https://img.aspxhome.com/file/UploadPic/200910/6/medium-36s.jpg)
详解如何基于Pyecharts绘制常见的直角坐标系图表
2022-07-08 05:18:31
![](https://img.aspxhome.com/file/2023/4/71424_0s.png)
详解如何用Python登录豆瓣并爬取影评
2021-09-08 00:10:10
![](https://img.aspxhome.com/file/2023/3/131153_0s.jpg)
Python 如何创建一个简单的REST接口
2022-03-07 17:03:16
pandas的Series类型与基本操作详解
2021-03-23 12:06:36
![](https://img.aspxhome.com/file/2023/7/99667_0s.png)
Python zip函数打包元素实例解析
2023-04-18 12:53:52
那些看一眼就让你难忘的广告
2007-09-21 19:46:00
![](https://img.aspxhome.com/file/UploadPic/20079/21/2007921195335406s.jpg)