爬取B站UP主信息
作者:互联网
爬取方式
直接搜索UP主,按F12打开开发者调试工具可以看到如下内容:
- 预览:可以看到每一个每一个URL下返回的信息
- 标头:可以看到对应的请求URL和响应等
- 响应:我们用requests.get方法得到返回的内容
- 用json.loads()可以将该内容转为Python的字典,方便提取信息
代码
这里我们爬取少部分信息用作测试。
import requests
import json
up_name = input('请输入UP主名字:')
url = 'https://api.bilibili.com/x/web-interface/search/type?__refresh__=true&_extra=&context=&page=1&page_size=36&order=&duration=&from_source=&from_spmid=333.337&platform=pc&highlight=1&single_column=0&keyword={}&category_id=&search_type=bili_user&order_sort=0&user_type=0&dynamic_offset=0&preload=true&com2co=true'.format(up_name)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
}
try:
response = requests.get(url=url, headers=headers) # 获取response响应
except:
print("spyder error")
data_str = response.text # 转字符串
data_dict = json.loads(data_str) # 转字典
# print(data_str)
basic_infom =data_dict['data']['result'][0] # 取第一个
# print(data_dict['data']['result'][0])
is_or_not = ['否','是']
basic_info = '''
UP:{}
UID:{}
等级:{}
简介:{}
粉丝数:{}
投稿数:{}
直播间ID:{}
是否在直播:{}
'''.format(basic_infom['uname'],basic_infom['mid'],basic_infom['level'],
basic_infom['usign'],basic_infom['fans'],basic_infom['videos'],
basic_infom['room_id'],is_or_not[basic_infom['is_live']])
print(basic_info)
运行测试
标签:infom,UP,信息,爬取,dict,basic,print,data 来源: https://www.cnblogs.com/hair-is-decreasing/p/16364268.html