This is the 17th day of my participation in the August More text Challenge. For details, see: August More Text Challenge
Directly on the code and renderings
Python requests for requests in requests (requests for requests in requests)
request
Remember to add the request header, the get method
headers = {
'Host': 'api.bilibili.com'.'Referer': 'https://www.bilibili.com/video/av77413543'.'User-Agent': 'the Mozilla / 5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
info = 'https://api.bilibili.com/x/web-interface/archive/stat?aid=' + av
info_rsp = requests.get(url=info, headers=headers)
Copy the code
parsing
Parse directly using JSON
info_rsp = requests.get(url=info, headers=headers)
info_json = info_rsp.json()
Copy the code
Then parse the response part of the comment
comment_url = 'https://api.bilibili.com/x/v2/reply'
page = 1
while True:
now_time = int(time.time() * 1000)
param = {
'callback': 'jQuery17205146934182044087_' + str(now_time),
'jsonp': 'jsonp'.'pn': page,
'type': '1'.'oid': av,
'sort': '2'.'_': now_time
}
rsp = requests.get(url=comment_url, headers=headers, params=param)
rsp_str = rsp.text.replace('jQuery17205146934182044087_' + str(now_time) + "(".' ').strip(') ')
com_json = json.loads(rsp_str)
Copy the code
save
And then the file here to save the operation
com_json['code'] = =0:
try:
replies = com_json['data'] ['replies']
print('Current {} page'.format(page))
for replie in replies:
# print(replie)
print('{}\n{}\n'.format('Nickname:' + replie['member'] ['uname'].'Comment content:' + replie['content'] ['message']))
fp.write('{}\n{}\n'.format('Nickname:' + replie['member'] ['uname'].'Comment content:' + replie['content'] ['message']))
page += 1
time.sleep(1)
Copy the code
The complete code
import requests
import re
import time
import json
def get_info() :
headers = {
'Host': 'api.bilibili.com'.'Referer': 'https://www.bilibili.com/video/av77413543'.'User-Agent': 'the Mozilla / 5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
info = 'https://api.bilibili.com/x/web-interface/archive/stat?aid=' + av
info_rsp = requests.get(url=info, headers=headers)
info_json = info_rsp.json()
with open(av + '_info.txt'.'w', encoding='utf-8') as fp:
if info_json['code'] = =0:
fp.write('Number of plays: {}\n'.format(info_json['data'] ['view']))
fp.write('Number of bullets: {}\n'.format(info_json['data'] ['danmaku']))
fp.write('Collection number: {}\n'.format(info_json['data'] ['favorite']))
fp.write('Number of coins: {}\n'.format(info_json['data'] ['coin']))
fp.write('Share number: {}\n'.format(info_json['data'] ['share']))
fp.write('Number of likes: {}\n'.format(info_json['data'] ['like']))
print('Number of views :', info_json['data'] ['view'])
print('Bullet count :', info_json['data'] ['danmaku'])
print('Likes :', info_json['data'] ['like'])
print('Collection number :', info_json['data'] ['favorite'])
print('Number of coins :', info_json['data'] ['coin'])
print(Number of forwards:, info_json['data'] ['share'])
fp.write('\n')
comment_url = 'https://api.bilibili.com/x/v2/reply'
page = 1
while True:
now_time = int(time.time() * 1000)
param = {
'callback': 'jQuery17205146934182044087_' + str(now_time),
'jsonp': 'jsonp'.'pn': page,
'type': '1'.'oid': av,
'sort': '2'.'_': now_time
}
rsp = requests.get(url=comment_url, headers=headers, params=param)
rsp_str = rsp.text.replace('jQuery17205146934182044087_' + str(now_time) + "(".' ').strip(') ')
com_json = json.loads(rsp_str)
print(a)if com_json['code'] = =0:
try:
replies = com_json['data'] ['replies']
print('Current {} page'.format(page))
for replie in replies:
# print(replie)
print('{}\n{}\n'.format('Nickname:' + replie['member'] ['uname'].'Comment content:' + replie['content'] ['message']))
fp.write('{}\n{}\n'.format('Nickname:' + replie['member'] ['uname'].'Comment content:' + replie['content'] ['message']))
page += 1
time.sleep(1)
except Exception as e:
break
# av = '77413543'
av = input('Please enter av number:')
get_info()
Copy the code