昨天看到個視頻,彈幕挺有意思的,于是想著用Python給他全部扒下來。
代碼非常簡單,接下來我們看看 具體操作。
需要準(zhǔn)備這些
軟件
- Python 3.8
- Pycharm
模塊使用
- import requests 數(shù)據(jù)請求
- import jieba 分詞
- import wordcloud 詞云
- import parsel 數(shù)據(jù)解析
- import re 正則
win + R 輸入cmd 輸入安裝命令 pip install 模塊名 (如果你覺得安裝速度比較慢, 你可以切換國內(nèi)鏡像源)
本次目標(biāo)
視頻地址: https://www.bilibili.com/video/BV1Nz4y1x7tA/
彈幕內(nèi)容: https://www.ibilibili.com/video/BV1Nz4y1x7tA/
彈幕地址: https://api.bilibili.com/x/v1/dm/list.so?oid=1205968547
流程步驟
基本實現(xiàn)步驟: <公式>
- 發(fā)送請求 --> 數(shù)據(jù)所對應(yīng)鏈接地址
https://api.bilibili.com/x/v1/dm/list.so?oid=1205968547 - 獲取數(shù)據(jù) --> 獲取整個頁面數(shù)據(jù)內(nèi)容
- 解析數(shù)據(jù) --> 提取具體數(shù)據(jù)內(nèi)容
- 保存數(shù)據(jù) --> 保存本地文件
代碼展示
模塊導(dǎo)入# 數(shù)據(jù)請求模塊 --> 第三方模塊 需要安裝 pip install requests
import requests # 導(dǎo)入正則表達式 --> 內(nèi)置模塊不需要安裝 import re # 導(dǎo)入數(shù)據(jù)解析模塊 --> 第三方模塊 需要安裝 pip install parsel
# 視頻講解+代碼我都打包好了,直接在這個q裙自取:708525271
import parsel
?
發(fā)送請求
# 請求鏈接 url = 'https://api.bilibili.com/x/v1/dm/list.so?oid=1205968547' # 調(diào)用requests模塊里面get請求方法對于url地址發(fā)送請求 response = requests.get(url) print(response)
?
獲取數(shù)據(jù)
獲取文本響應(yīng)數(shù)據(jù)
print(response.text)
?
當(dāng)我們獲取數(shù)據(jù)出現(xiàn)亂碼怎么辦呢?
response .encoding = 'utf-8'
?
數(shù)據(jù)解析
正則匹配數(shù)據(jù)
re_content = re.findall('<d p=".*?">(.*?)</d>',html_data) content = re.findall('<source>(.*?)</source>', html_data) print(re_content) print(content)
?
轉(zhuǎn)化數(shù)據(jù)
把獲取到響應(yīng)文本數(shù)據(jù),轉(zhuǎn)成可解析對象。
selector = parsel.Selector(html_data) # 選擇器對象 # d 標(biāo)簽名字 d::text -提取d標(biāo)簽里的文本內(nèi)容 css_content = selector.css('d::text').getall() print(selector)
?
保存數(shù)據(jù)
for content in css_content: # mode 保存方式 --> w寫入數(shù)據(jù), 會覆蓋 a 追加保存 with open('彈幕_1.txt', mode='a', encoding='utf-8') as f: f.write(content) f.write('\n') print(content)
?文章來源:http://www.zghlxwxcb.cn/news/detail-626240.html
好了,今天的分享就到這里結(jié)束了,咱們下次見!文章來源地址http://www.zghlxwxcb.cn/news/detail-626240.html
到了這里,關(guān)于簡單的用Python獲取一下視頻彈幕,新手練手實戰(zhàn)項目,非常簡單!的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!