爬取视频专栏每集名称与时长

  1. import requests
  2. import pandas as pd
  3. url = 'https://api.bilibili.com/x/player/pagelist?bvid=BV12b411K7Zu&jsonp=jsonp'
  4. headers = {
  5. 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 '
  6. 'Safari/537.36 '
  7. }
  8. res = requests.get(url=url, headers=headers).json()['data']
  9. print(res)
  10. df = pd.DataFrame(columns=['集名', '时长/秒'])
  11. for row in res:
  12. df = pd.concat([df, pd.DataFrame({
  13. '集名': ['P' + str(row['page']) + ' ' + row['part']],
  14. '时长/秒': [row['duration']]
  15. })], ignore_index=True)
  16. df.to_excel('B站视频集名与时长.xlsx')