爬取微博评论遇到JSONDecodeError错误

python小白一个,原本打算爬取微博评论帮助论文撰写,在B站上看了相关视频教学后写了代码,爬到了一页的评论,后来发现再往下翻页时url发生改变(不是单纯的type=0改变),找到下一页的url后运行代码出现JSONDecodeError,已经在网上搜索了相关问题,基本有关帖子都看过了,了解到可能是json有关的文件格式问题?我也根据帖子回答进行了尝试,均未能解决问题,自己也对PYTHON不是很懂,请求懂的朋友帮助解决!感激不尽这是我想爬取的微博链接

import requests
import json
import pprint
#url = 'https://m.weibo.cn/comments/hotflow?id=4466452846934997&mid=4466452846934997&max_id_type=0'(这个url我运行成功了)
url = 'https://m.weibo.cn/comments/hotflow?id=4466452846934997&mid=4466452846934997&max_id=154784122838228&max_id_type=0'#运行失败
response = requests.get(url)
data = json.loads(response.text,strict = False)#这是line7
pprint.pprint(data)
users = data['data']['data']
for user in users:
    print(user["text"])
    with open('7.csv', mode='a', encoding='utf-8-sig',errors='ignore') as f:
        f.write(user["text"])
        f.write('\n')
下面是报错:
Traceback (most recent call last):
  File "C:/Users/ming/Desktop/评论/venv/44444.py", line 7, in <module>
    data = json.loads(response.text,strict = False)
  File "D:\python\lib\json\__init__.py", line 332, in loads
    return cls(**kw).decode(s)
  File "D:\python\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\python\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Process finished with exit code 1
讨论数量: 2
Jason990420

m.weibo.cn/comments/hotflow?id=446...

返回的是 json

m.weibo.cn/comments/hotflow?id=446...

返回的是 html (要求登录), 非 json 格式, 所以 JSONDecodeError

1周前 评论
baiming (楼主) 1周前
baiming (楼主) 1周前
Jason990420

Maybe it work for you

Python爬虫 抓取微博评论

1周前 评论

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!