bilibili番剧评分爬虫

Posted kagi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bilibili番剧评分爬虫相关的知识,希望对你有一定的参考价值。

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

以上是关于bilibili番剧评分爬虫的主要内容,如果未能解决你的问题,请参考以下文章

IOS-高仿bilibili项目

Python爬虫实战篇bilibili视频弹幕提取

python基础 爬虫项目都有哪些?

用python爬虫爬取携程网国内租车怎么获取所在地址的车辆信息、价格还有评分并安型号分类提取出来?

Bilibili是一款怎样的产品?

Python爬虫:Bilibili模拟登陆(滑动验证码)项目