python基于selenium爬取斗魚彈幕
針對彈幕的爬取我們?nèi)绻恍枰@取看到的網(wǎng)頁里面的而數(shù)據(jù),使用selenium就能實(shí)現(xiàn),對于直播平臺來說,往往有第三方平臺api讓你獲取數(shù)據(jù)(可以獲取發(fā)彈幕,發(fā)彈幕者的名字禮物等等,這需要客戶端向彈幕服務(wù)器發(fā)送登錄請求,心跳信息的發(fā)送等等)只獲取彈幕信息儲存到txt文件中,上代碼,上圖片
代碼如下:
import timefrom selenium import webdriverchrome_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: continuedef 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爬取斗魚彈幕的詳細(xì)內(nèi)容,更多關(guān)于python 爬取斗魚彈幕的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. WML語法大全與相關(guān)介紹第1/3頁2. 使用css實(shí)現(xiàn)全兼容tooltip提示框3. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)4. 匹配模式 - XSL教程 - 45. 詳解JS前端使用迭代器和生成器原理及示例6. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁7. 詳解CSS偽元素的妙用單標(biāo)簽之美8. ASP常用日期格式化函數(shù) FormatDate()9. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向10. asp中response.write("中文")或者js中文亂碼問題
