news/weibo_api/weibo_api.py
2022-12-24 12:52:18 +08:00

51 lines
1.5 KiB
Python

import requests
import json
import time
API='https://weibo.com/ajax/side/hotSearch'
headers={
'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Headers':'Content-Type',
'Access-Control-Allow-Methods':'*',
'Content-Type':'application/json;charset=utf-8'
}
def get_topic():
try:
dataList=[]
data=requests.get(API,headers=headers,timeout=5)
data=json.loads(data.text)
data_json=data['data']['realtime']
jyzy = {
'电影': '',
'剧集': '',
'综艺': '',
'音乐': '',
'盛典': '',
}
for i in range(0,len(data_json)):
hot = ''
if 'is_ad' in data_json[i]:
continue;
if 'flag_desc' in data_json[i]:
hot = jyzy.get(data_json[i]['flag_desc'],'')
if 'is_boom' in data_json[i]:
hot = ''
if 'is_hot' in data_json[i]:
hot = ''
if 'is_fei' in data_json[i]:
hot = ''
if 'is_new' in data_json[i]:
hot = ''
dic = {
'title': data_json[i].get('note',''),
'url': 'https://s.weibo.com/weibo?q=%23' + data_json[i].get('word','') + '%23',
'num': data_json[i].get('num',''),
'hot': hot
}
dataList.append(dic)
return dataList
except Exception as e:
print(str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())),e)
return None