最近在小破站热门上看见了这个:
不得不说还是挺不一样的
对吧?什么你说不是?
哎呦~ 你干嘛 ~
咳咳…话不多说,让我们一起来康康~
源码、资料电子书点击这里
安装Python并添加到环境变量,
pip安装需要的相关模块即可。
现在点击历史弹幕数据,同样是有数据加载出来,但是里面的都是乱码了。
弹幕分页是根据日期来的,当点击日期的使用,
返回的给我的数据并不是弹幕数据,而是所有的日期。
那么看到这里有人就会问了,
那我想要爬取目标日期的弹幕数据怎么办?
这两个的url地址是不一样的,
seg.so
才是弹幕数据url地址。
import requests
import re
def get_response(html_url):
headers = {
'cookie': '你自己的cookie',
'origin': 'https://www.****',
'referer': 'https://****/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.****.com/x/v2/dm/history/index?type=1&oid=120004475&month=2021-01'
get_date(one_url)
返回的数据是json数据,
根据字典键值对取值就可以得到相关数据。
def main(html_url):
data = get_date(html_url)
for date in data:
url = f'https://****/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('**弹幕.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)