python基于selenium爬取斗鱼弹幕
作者:Martina_oh 时间:2022-08-30 16:36:19
针对弹幕的爬取我们如果只需要获取看到的网页里面的而数据,使用selenium就能实现,对于直播平台来说,往往有第三方平台api让你获取数据(可以获取发弹幕,发弹幕者的名字礼物等等,这需要客户端向弹幕服务器发送登录请求,心跳信息的发送等等)只获取弹幕信息储存到txt文件中,上代码,上图片
代码如下:
import time
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
# 使用headless * 面浏览器模式
# chrome_options.add_argument('--headless')
# chrome_options.add_argument('--disable-gpu')
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
browser = webdriver.Chrome(chrome_options=chrome_options)
url = 'https://www.douyu.com/'
def getDanmu(homeId):
homeHref = url+str(homeId)
browser.get(homeHref)
while 1:
time.sleep(2)
try:
for i in browser.find_elements_by_xpath('.//div[@class=" danmu-6e95c1"]/div/div'):
if len(i.text) > 0:
try:
print(i.text)
except:
pass
saveDanmu(i.text)
else:
continue
except:
time.sleep(2)
for i in browser.find_elements_by_xpath('.//div[@class=" danmu-6e95c1"]/div/div'):
if len(i.text) > 0:
try:
print(i.text)
except:
pass
saveDanmu(i.text)
else:
continue
def saveDanmu(danmu):
with open('danmu.txt', 'a+', encoding='utf-8')as f:
f.write(danmu+'\n')
if __name__ == '__main__':
num = input('请输入需要查询的房间号:')
getDanmu(num)
来源:https://www.cnblogs.com/Martinaoh/p/14265300.html
标签:python,爬虫,斗鱼,selenium
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 如何在 Matplotlib 中绘制垂直线
2021-05-29 05:38:10
![](https://img.aspxhome.com/file/2023/0/85020_0s.png)
Python:type、object、class与内置类型实例
2023-09-27 08:51:27
ASP环境中使用QQ纯真版IP数据库QQWry.dat
2010-02-26 13:35:00
iis、apache与nginx禁止目录执行asp、php脚本的实现方法
2023-10-14 11:30:26
![](https://img.aspxhome.com/file/2023/7/64587_0s.gif)
Python基于pyCUDA实现GPU加速并行计算功能入门教程
2021-11-01 08:24:58
![](https://img.aspxhome.com/file/2023/2/85332_0s.jpg)
利用box-sizing实现div仿框架
2009-12-08 15:45:00
![](https://img.aspxhome.com/file/UploadPic/200912/8/01-32s.gif)
Django url,从一个页面调到另个页面的方法
2021-08-08 03:51:51
运行(runCode)复制(copyCode)保存(saveCode)代码框方法
2007-10-21 08:41:00
python怎么判断素数
2021-09-30 11:10:33
Python实现的HMacMD5加密算法示例
2022-01-15 06:05:31
Python实现绘制M2货币供应率曲线
2023-07-31 04:30:43
![](https://img.aspxhome.com/file/2023/1/77601_0s.jpg)
交互设计实用指南系列(3)—“有效性”之“适时帮助”
2009-12-25 14:29:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/04d4365a20d52fbefe705991235bec36-87s.gif)
Python实现删除重复文件的示例代码
2022-02-26 03:50:22
Python 将 QQ 好友头像生成祝福语的实现代码
2022-01-11 20:29:33
![](https://img.aspxhome.com/file/2023/2/104922_0s.png)
设计良好网页的4项原则
2009-04-24 12:48:00
![](https://img.aspxhome.com/file/UploadPic/20094/24/01-51s.jpg)
数据分页显示按钮,自动获取url链接
2009-06-22 12:59:00
Python中max函数用于二维列表的实例
2022-07-04 15:49:55
Python对接支付宝支付自实现功能
2023-09-19 13:57:51
ASP伪静态页简单教程
2007-09-28 14:35:00
十“问”DreamWeaver
2007-02-03 11:39:00