python選修課學習中練手寫的,主要就是查詢bilibili提供得api 整理結果csv https://pan.baidu.com/s/1jHX2fJ4 ...
python選修課學習中練手寫的,主要就是查詢bilibili提供得api
# -*- coding:utf-8 -*- import requests import json import csv import sys #將windows系統預設語言從gbk-2312設置為utf-8 reload(sys) sys.setdefaultencoding('utf-8') def rating(bangumi_id): payload = {'callback': 'seasonListCallback'} response = requests.get('https://bangumi.bilibili.com/jsonp/seasoninfo/{0}.ver'.format(bangumi_id), params=payload) data = json.loads(response.text[19:-2]) try: season_id = int(data['result']['season_id']) title = '{0}'.format(data['result']['media']['title']) score = float(data['result']['media']['rating']['score']) count = int(data['result']['media']['rating']['count']) is_finish = int(data['result']['is_finish']) try: writer.writerow([season_id, title, score, count, is_finish]) except sqlite3.IntegrityError: pass except KeyError: try: season_id = int(data['result']['season_id']) title = '{0}'.format(data['result']['title']) score=float(0) count=int(0) is_finish = int(data['result']['is_finish']) try: writer.writerow([season_id, title, score, count, is_finish]) except sqlite3.IntegrityError: pass except KeyError: return None return None if __name__ == '__main__': with open('bangumi.csv', 'wb+') as csv_file: writer = csv.writer(csv_file, delimiter=',') writer.writerow(['序號','名稱', '評分(預設0分)', '評分人數(人數不足為0人)', '是否完結(1:表示已完結)']) for i in range(7000): rating(i)
整理結果csv https://pan.baidu.com/s/1jHX2fJ4