运行txet可以爬取数据,转换json就不行了
新建的其他代码json可以运行
url = 'https://movie.douban.com/typerank'
param = {
"type_name": "动作",
"type": 5,
"interval_id": "100:90",
"action":"",
}
heards = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
}
resp = requests.get(url,params=param,headers=heards)
print(resp.json())
下面是报错
Traceback (most recent call last):
File "C:\Users\陈北\PycharmProjects\pythonProject\venv\Lib\site-packages\requests\models.py", line 971, in json
return complexjson.loads(self.text, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\陈北\Desktop\py所有文件\Lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\陈北\Desktop\py所有文件\Lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\陈北\Desktop\py所有文件\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)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\陈北\PycharmProjects\pythonProject\1.爬虫\2.2.py", line 16, in <module>
print(resp.json())
^^^^^^^^^^^
File "C:\Users\陈北\PycharmProjects\pythonProject\venv\Lib\site-packages\requests\models.py", line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
进程已结束,退出代码1
下面resp/text就可以。
C:\Users\陈北\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\陈北\PycharmProjects\pythonProject\1.爬虫\2.2.py
html>
<html lang="zh-CN" class="ua-windows ua-webkit">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="renderer" content="webkit">
<meta name="referrer" content="always">
<meta name="google-site-verification" content="ok0wCgT20tBBgo9_zat2iAcimtN4Ftf5ccsh092Xeyw" />
<title>豆瓣电影分类排行榜 - 动作片title>
他就不是个json格式的数据,就是个html,当然不能用 .json获取
不知道你这个问题是否已经解决, 如果还没有解决的话: