请问一下,我这段代码哪里有错误

import requests
data={'data':'dog'}
header={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
post_url="https://fanyi.baidu.com/?aldtype=16047#auto/zh"
response=requests.post(url=post_url,data=data,headers=header)
response_json = response.json()
fp=open('爬虫实验2.json','w',)
json.dump(response_json,fp=fp,ensure_ascii=False)

 

 

一直显示Traceback (most recent call last):
  File "E:\编程\python\爬虫\爬虫实验2.py", line 6, in <module>
    response_json = response.json()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

最后一行改成这个再试试

response_json.dump(response_json, fp=fp, ensure_ascii=False)

 

post能正常返回页面信息,但不是json格式的,所以报json解码错误,你将response_json = response.text,print(response_json)就可看到。需要在header中添加参数比如cookie(token)或其他参数