這篇文章主要介紹“Python怎么爬取B站視頻彈幕”,在日常操作中,相信很多人在Python怎么爬取B站視頻彈幕問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python怎么爬取B站視頻彈幕”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比獲嘉網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式獲嘉網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋獲嘉地區(qū)。費(fèi)用合理售后完善,十載實體公司更值得信賴。
Python 3.6
Pycharm
requests
re
安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可。
找一個彈幕比較多的視頻爬取
以前的B站彈幕視頻,點(diǎn)擊查看歷史的彈幕,會給你返回一個json數(shù)據(jù),包含了所有的彈幕內(nèi)容。
現(xiàn)在點(diǎn)擊歷史彈幕數(shù)據(jù),同樣是有數(shù)據(jù)加載出來,但是里面的都是亂碼了。
請求這個鏈接還是會得到想要的數(shù)據(jù)內(nèi)容。
只需要使用正則表達(dá)匹配中文字符就可以匹配出來
彈幕分頁是根據(jù)日期來的,當(dāng)點(diǎn)擊 2021-01-01 的使用,返回的給我的數(shù)據(jù)并不是彈幕數(shù)據(jù),而是所有的日期。
那么看到這里有人就會問了,那我想要爬取 2021-01-01 的彈幕數(shù)據(jù)怎么辦?
這兩個的url地址是不一樣的,seg.so 才是彈幕數(shù)據(jù)url地址。
import requests import re def get_response(html_url): headers = { 'cookie': '你自己的cookie', 'origin': 'https://www.bilibili.com', 'referer': 'https://www.bilibili.com/video/BV19E41197Kc', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36', } response = requests.get(url=html_url, headers=headers) return response def get_date(html_url): response = get_response(html_url) json_data = response.json() date = json_data['data'] print(date) return date if __name__ == '__main__': one_url = 'https://api.bilibili.com/x/v2/dm/history/index?type=1&oid=120004475&month=2021-01' get_date(one_url)
返回的數(shù)據(jù)是json數(shù)據(jù),根據(jù)字典鍵值對取值就可以得到相關(guān)數(shù)據(jù)。
def main(html_url): data = get_date(html_url) for date in data: url = f'https://api.bilibili.com/x/v2/dm/web/history/seg.so?type=1&oid=120004475&date={date}' html_data = get_response(url).text result = re.findall(".*?([\u4E00-\u9FA5]+).*?", html_data) for i in result: with open('B站彈幕.txt', mode='a', encoding='utf-8') as f: f.write(i) f.write('\n')
import requests import re def get_response(html_url): headers = { 'cookie': '你自己的cookie', 'origin': 'https://www.bilibili.com', 'referer': 'https://www.bilibili.com/video/BV19E41197Kc', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36', } response = requests.get(url=html_url, headers=headers) return response def get_date(html_url): response = get_response(html_url) json_data = response.json() date = json_data['data'] print(date) return date def save(content): for i in content: with open('B站彈幕.txt', mode='a', encoding='utf-8') as f: f.write(i) f.write('\n') print(i) def main(html_url): data = get_date(html_url) for date in data: url = f'https://api.bilibili.com/x/v2/dm/web/history/seg.so?type=1&oid=120004475&date={date}' html_data = get_response(url).text result = re.findall(".*?([\u4E00-\u9FA5]+).*?", html_data) save(result) if __name__ == '__main__': one_url = 'https://api.bilibili.com/x/v2/dm/history/index?type=1&oid=120004475&month=2021-01' main(one_url)
到此,關(guān)于“Python怎么爬取B站視頻彈幕”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
當(dāng)前文章:Python怎么爬取B站視頻彈幕
標(biāo)題URL:http://chinadenli.net/article30/gicepo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、全網(wǎng)營銷推廣、網(wǎng)站改版、用戶體驗、、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)