前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理,
前文內容
Python爬蟲新手入門教學(一):爬取豆瓣電影排行資訊
Python爬蟲新手入門教學(二):爬取小說
Python爬蟲新手入門教學(三):爬取鏈家二手房資料
Python爬蟲新手入門教學(四):爬取前程無憂招聘資訊
Python爬蟲、資料分析、網站開發等案例教程視頻免費在線觀看
https://space.bilibili.com/523606542
基本開發環境
- Python 3.6
- Pycharm
相關模塊的使用
- requests
- re
安裝Python并添加到環境變數,pip安裝需要的相關模塊即可,
一、明確需求
找一個彈幕比較多的視頻爬取
二、網頁資料分析
以前的B站彈幕視頻,點擊查看歷史的彈幕,會給你回傳一個json資料,包含了所有的彈幕內容,
現在點擊歷史彈幕資料,同樣是有資料加載出來,但是里面的都是亂碼了,
請求這個鏈接還是會得到想要的資料內容,
只需要使用正則表達匹配中文字符就可以匹配出來
三、決議資料并多頁爬取
彈幕分頁是根據日期來的,當點擊 2021-01-01 的使用,回傳的給我的資料并不是彈幕資料,而是所有的日期,
那么看到這里有人就會問了,那我想要爬取 2021-01-01 的彈幕資料怎么辦?
這兩個的url地址是不一樣的,seg.so 才是彈幕資料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 = https://www.cnblogs.com/hhh188764/archive/2021/01/26/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)
回傳的資料是json資料,根據字典鍵值對取值就可以得到相關資料,
四、保存資料(資料持久化)
def main(html_url):
data = https://www.cnblogs.com/hhh188764/archive/2021/01/26/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 = https://www.cnblogs.com/hhh188764/archive/2021/01/26/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 = https://www.cnblogs.com/hhh188764/archive/2021/01/26/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 = https://www.cnblogs.com/hhh188764/archive/2021/01/26/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 = https://www.cnblogs.com/hhh188764/archive/2021/01/26/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)
總結
1、需要登陸才能查看歷史彈幕,爬取時需要攜帶cookie
2、可以保存到Excel里面,本文是保存txt文本
3、保存資料之后可以做詞云分析下篇文章再說吧
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/252914.html
標籤:其他

