From 88dead62e28d8d5d00e9d7fc897eea6e1343a15a Mon Sep 17 00:00:00 2001 From: felinae98 <731499577@qq.com> Date: Wed, 3 Mar 2021 20:14:25 +0800 Subject: [PATCH] fix get weibo error --- .../nonebot_hk_reporter/platform/weibo.py | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/plugins/nonebot_hk_reporter/platform/weibo.py b/src/plugins/nonebot_hk_reporter/platform/weibo.py index c83a488..f120fc8 100644 --- a/src/plugins/nonebot_hk_reporter/platform/weibo.py +++ b/src/plugins/nonebot_hk_reporter/platform/weibo.py @@ -102,15 +102,30 @@ class Weibo(Platform): return res async def parse(self, raw_post: RawPost) -> Post: + header = { + 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', + 'accept-language': 'zh-CN,zh;q=0.9', + 'authority': 'm.weibo.cn', + 'cache-control': 'max-age=0', + 'sec-fetch-dest': 'empty', + 'sec-fetch-mode': 'same-origin', + 'sec-fetch-site': 'same-origin', + 'upgrade-insecure-requests': '1', + 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) ' + 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 ' + 'Mobile Safari/537.36'} if raw_post.get('_type') == 50: # cancel top return Post('weibo', text="撤置顶", url='', pics=[], target_name=raw_post['target'], override_use_pic=False) info = raw_post['mblog'] if info['isLongText'] or info['pic_num'] > 9: async with httpx.AsyncClient() as client: - res = await client.get('https://m.weibo.cn/detail/{}'.format(info['mid'])) - full_json_text = re.search(r'"status": ([\s\S]+),\s+"hotScheme"', res.text).group(1) - info = json.loads(full_json_text) + res = await client.get('https://m.weibo.cn/detail/{}'.format(info['mid']), headers=header) + try: + full_json_text = re.search(r'"status": ([\s\S]+),\s+"hotScheme"', res.text).group(1) + info = json.loads(full_json_text) + except: + logger.info('detail message error: https://m.weibo.cn/detail/{}'.format(info['mid'])) parsed_text = self._get_text(info['text']) pic_urls = [img['large']['url'] for img in info.get('pics', [])] detail_url = 'https://weibo.com/{}/{}'.format(info['user']['id'], info['bid'])