简体   繁体   English

Python请求Json数据,想要将其从Excel复制回csv文件

[英]Python request Json data, want to copy it back into csv file from excel

This is the URL I'm trying to use python to pull back the json data from: 这是我正在尝试使用python从以下位置拉回json数据的URL:

https://www.fangraphs.com/leaders.aspx?pos=all&stats=bat&lg=all&qual=y&type=8&season=2018&month=0&season1=2018&ind=0 https://www.fangraphs.com/leaders.aspx?pos=all&stats=bat&lg=all&qual=y&type=8&season=2018&month=0&season1=2018&ind=0

This is the code (keep in mind I'm very new to learning Python): 这是代码(请记住,我是学习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)

Am I setting this up right? 我可以正确设置吗?

It looks like your response content is not in JSON notation, if you use print(resp.headers) you should see something like: 看起来您的响应内容不是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'
}

As you can see the Content-Type is NOT JSON, so the decoder can't parse it. 如您所见,Content-Type不是NOT JSON,因此解码器无法解析它。 You might need to use BeautifulSoup or some other scraping solution. 您可能需要使用BeautifulSoup或其他一些抓取解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM