【发布时间】:2018-03-15 06:59:56
【问题描述】:
我正在尝试从 Google 财经的 JSON 文件中提取信息。 requests.get() 正在工作,但后来我被卡住了。我已经搜索了很多,似乎没有任何建议有效。这就是我所拥有的:
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)
输出是“200”,我相信这是正常的输出。 print(response.content) 给了我完整的 JSON 字符串,所以它似乎工作正常。
但是,尝试将其传递给“数据”以便我可以进一步处理,提取各种位。这是我尝试过的:
data = response.json()给我JSONDecodeError: Expecting value: line 2 column 1 (char 1)data = json.load(response)给我AttributeError: 'Response' object has no attribute 'read'data = json.loads(response)给我TypeError: the JSON object must be str, bytes or bytearray, not 'Response'
我尝试了data = json.loads(response.decode("utf-8")),这给了我AttributeError: 'Response' object has no attribute 'decode'。我也尝试了一些文本清理建议,但没有任何效果。
【问题讨论】:
标签: python json python-3.x python-requests