Bilibili.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import json
  2. import requests
  3. import re
  4. url = 'https://www.bilibili.com/video/BV1H7421T7zx/?spm_id_from=333.337.search-card.all.click&vd_source=f6247aa12dae1ff1bce74ef0af381757'
  5. def GetResponse(url):
  6. headers = {
  7. 'accept': 'text / html, application / xhtml + xml, application / xml;q = 0.9, image / avif, image / webp, image / apng, * / *;q = 0.8, application / signed - exchange;v = b3;q = 0.7',
  8. 'accept - encoding': 'gzip, deflate',
  9. 'accept - language': 'zh - CN, zh;q = 0.9, en;q = 0.8, en - GB;q = 0.7, en - US;q = 0.6',
  10. 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36',
  11. 'Referer':'https://search.bilibili.com/all?vt=44591831&keyword=%E8%B0%81%E7%94%B5%E5%90%89%E4%BB%96%E8%B0%B1&from_source=webtop_search&spm_id_from=333.1007&search_source=2'
  12. }
  13. response = requests.get(url = url,headers = headers)
  14. return response
  15. def GetVideoInfo():
  16. response = GetResponse(url = url)
  17. html = response.text
  18. info = re.findall('<script>window.__playinfo__=(.*?)</script>', html)[0]
  19. json_data = json.loads(info)
  20. audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
  21. video_url = json_data['data']['dash']['video'][0]['baseUrl']
  22. title = re.findall('<title data-vue-meta="true">(.*?)</title>', html)
  23. if title:
  24. title = title[0]
  25. else:
  26. return None
  27. return (title, audio_url, video_url)
  28. def Save(title,audio_url,video_url):
  29. audio_content = GetResponse(url = audio_url).content
  30. video_content = GetResponse(url = video_url).content
  31. with open('bili\\' + title + '.mp3', mode = 'wb') as audio:
  32. audio.write(audio_content)
  33. with open('bili\\' + title + '.mp4', mode = 'wb') as video:
  34. video.write(video_content)
  35. if __name__ == '__main__':
  36. title,audio_url,video_url = GetVideoInfo()
  37. Save(title, audio_url, video_url)