[英]JSON not working from Google Finance
I am trying to extract information from a JSON file from Google Finance. 我正在尝试从Google财经从JSON文件中提取信息。 The
requests.get()
is working but then I get stuck. requests.get()
正在工作,但后来我卡住了。 I have searched quite a bit and nothing suggested seems to work. 我已经搜索了很多,似乎没有任何建议起作用。 This is what I have:
这就是我所拥有的:
import requests
from json import loads
params={'q': 'NASDAQ:AAPL','output': 'json'}
response = requests.get('https://finance.google.com/finance', params=params, allow_redirects=False, timeout=10.0)
print(response.status_code)
print(response.content)
The output is “200” which is the ok output I believe. 输出是“ 200”,这是我认为可以的正常输出。
print(response.content)
gives me the full JSON string so that seems to be working ok. print(response.content)
为我提供了完整的JSON字符串,因此似乎可以正常工作。
However, trying to pass it into “data” so that I can work with further, extract various bits. 但是,尝试将其传递给“数据”,以便我可以进一步处理,提取各种位。 This is what I have tried:
这是我尝试过的:
data = response.json()
gives me JSONDecodeError: Expecting value: line 2 column 1 (char 1)
data = response.json()
给我JSONDecodeError: Expecting value: line 2 column 1 (char 1)
data = json.load(response)
gives me AttributeError: 'Response' object has no attribute 'read'
data = json.load(response)
给我AttributeError: 'Response' object has no attribute 'read'
data = json.loads(response)
gives me TypeError: the JSON object must be str, bytes or bytearray, not 'Response'
data = json.loads(response)
给我TypeError: the JSON object must be str, bytes or bytearray, not 'Response'
I tried data = json.loads(response.decode("utf-8"))
and that gives me AttributeError: 'Response' object has no attribute 'decode'
. 我尝试了
data = json.loads(response.decode("utf-8"))
,这给了我AttributeError: 'Response' object has no attribute 'decode'
。 I have also tried some text scrubbing suggestions, nothing has worked yet. 我还尝试了一些擦洗文本的建议,但还没有任何效果。
I printed the text. 我打印了文字。 and I found that first some data is not json string... \\n // chars...
我发现首先一些数据不是json字符串... \\ n //字符...
jsonstr = response.text[4:] #remove first part (not json data)
data = loads(jsonstr)
print(data)
print("t=",data[0]['t'])
output 输出
[{'t': 'AAPL', 'kr_annual_date': '2017', 'hi': '180.52', 'keyratios': [{'title': 'Net profit margin', 'annual': '21.09%', 'recent_quarter': '25.67%', 'ttm': '22.21%'}, {'title': 'Operating margin', 'annual': '26.76%', 'rece ....
....
com/'}]}]
t= AAPL
Try using response.content
or response.text
to convert to JSON. 尝试使用
response.content
或response.text
转换为JSON。
Ex: 例如:
json.loads(response.content)
or 要么
json.loads(response.text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.