【发布时间】:2021-07-30 06:40:57
【问题描述】:
您好,我正在使用 Python 脚本从澳大利亚证券交易所(JSON 格式)抓取股票价格信息。他们中的大多数都有从这个页面源获得的信息作为示例 https://www.asx.com.au/asx/1/share/tcl
但有些不是,但可以从https://www.asx.com.au/asx/1/share/tcl/prices?interval=daily&count=1 获得一部分信息(总比没有好)
第一个 url 我没有问题通过并从 json 中提取元素,但是第二个我无法获取任何元素。我认为问题在于第二个开头有“{”data”:[”。我尝试去除前 9 个字符(和最后 2 个),但这会导致不可下标的对象错误。
{"data":[{"code":"TCL","close_date":"2021-05-07T00:00:00+1000","close_price":14,"change_price":-0.1,"volume":2945117,"day_high_price":14.05,"day_low_price":13.87,"change_in_percent":"-0.709%"}]}
为了比较,第一个有效的响应是
{"code":"TCL","isin_code":"AU000000TCL6","desc_full":"Fully Paid Ordinary/units Stapled Securities","last_price":14,"open_price":13.96,"day_high_price":14.05,"day_low_price":13.87,"change_price":-0.1,"change_in_percent":"-0.709%","volume":2945117,"bid_price":13.95,"offer_price":14.05,"previous_close_price":14.1,"previous_day_percentage_change":"0.356%","year_high_price":15.635,"last_trade_date":"2021-05-07T00:00:00+1000","year_high_date":"2020-11-11T00:00:00+1100","year_low_price":12.36,"year_low_date":"2021-02-26T00:00:00+1100","year_open_price":6.91537,"year_open_date":"2014-02-25T11:00:00+1100","year_change_price":7.08463,"year_change_in_percentage":"102.448%","pe":0,"eps":-0.264,"average_daily_volume":4870346,"annual_dividend_yield":2.21,"market_cap":38607345120,"number_of_shares":2738109583,"deprecated_market_cap":38333534000,"deprecated_number_of_shares":2738109583,"suspended":false}
我为测试而拆分的代码子集如下
import requests
import json
import datetime
x = requests.get('https://www.asx.com.au/asx/1/share/tcl/prices?interval=daily&count=1')
if 'close_date' in x.json():
print(x.json()['close_date'][:10])
if 'close_price' in x.json():
print(x.json()['close_price'])
if 'change_in_percent' in x.json():
print(x.json()['change_in_percent'])
运行时我没有收到任何错误,但即使它们在 json 中,它也不会打印这些值
任何有关如何解决此问题的建议将不胜感激。我相信在响应中开始 9 个字符和最后 2 个字符是我的问题(但可能是非常错误的)。我觉得我几乎需要将 json 转换为字符串,清理它,然后将其转换回 json,但搜索并没有给我任何帮助的方向。
【问题讨论】:
-
即使它们在 json 中,它也不会打印这些值 是的,但它们不在顶层。它们是嵌套的。