这是我尝试使用 python 从中提取 json 数据的 URL:
这是代码(请记住,我对学习 Python 还很陌生):
import requests
url = 'https://www.fangraphs.com/leaders.aspx?'
params = dict(
pos='all',
stats='bat',
lg='all',
qual='y',
type='8',
season='2018',
month='0',
season1='2018',
ind='0'
)
resp = requests.get(url=url, params=params)
data = resp.json()
print(data)
我设置对了吗?
最佳答案
看起来您的响应内容不是 JSON 表示法,如果您使用 print(resp.headers) 您应该看到类似以下内容:
{
'Cache-Control': 'private',
'Content-Type': 'text/html; charset=utf-8',
'Server': 'Microsoft-IIS/10.0',
'X-AspNet-Version': '4.0.30319',
'X-Powered-By': 'ASP.NET',
'Date': 'Thu, 07 Feb 2019 17:29:33 GMT',
'Content-Length': '313209'
}
如您所见,Content-Type 不是 JSON,因此解码器无法解析它。 您可能需要使用 BeautifulSoup 或其他一些抓取解决方案。
关于Python请求Json数据,想将其从excel复制回csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54578118/