针对弹幕的爬取我们如果只需要获取看到的网页里面的而数据,使用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)
以上就是python基于selenium爬取斗鱼弹幕的详细内容,更多关于python 爬取斗鱼弹幕的资料请关注www.887551.com其它相关文章!
黄山市民网:https://www.huangshanshimin.com/