import json
import requests
class Weibo(object):
def __init__(self):
self.url = 'https://m.weibo.cn/api/container/getIndex?'
self.config = {
'search': 'containerid=100103type%3D1%26q%3D{}&page_type=searchall',
}
user_agent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Mobile Safari/537.36 Edg/117.0.2045.31"
self.headers = {"User-Agent": user_agent}
def search(self, target):
headers = {
"User-Agent": self.headers['User-Agent'],
"Accept": "application/json, text/plain, */*"
}
url = self.url + self.config['search'].format(target)
print('get: {}'.format(url))
r = requests.get(url, headers=headers)
return r.json()
print('init the weibo client')
weiboClient = Weibo()
res = weiboClient.search('2021年河南暴雨')
for i in res['data']['cards']:
if i['card_type'] == 9:
print(i["mblog"]['text'])
elif i['card_type'] == 11:
for j in i["card_group"]:
if j['card_type'] == 9:
print(j["mblog"]['text'])