Hi I am using a python script to scrape stock price information from the Australian Stock Exchange (JSON format). Most of them have information available from this page source as an example https://www.asx.com.au/asx/1/share/tcl
Some however aren't but a subset of information (which is better than nothing) is available from https://www.asx.com.au/asx/1/share/tcl/prices?interval=daily&count=1
The first url I have no issue going through and pulling the elements from the json however the second one I can't get any of the elements. I believe the issue is that the second one has "{"data":[" at the start. I have tried stripping first nine characters (and last 2) but that results in a not subscriptable object error.
{"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%"}]}
For comparison the response from the first one which works is
{"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}
a subset of my code that I have split out for testing is as follows
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'])
When run I don't get any error but it doesn't print those values even though they are in the json
Any suggestions on how to fix this would be appreciated. I believe that starting 9 characters and last 2 in the response is what my issue is (but could be very wrong). I feel like I almost like I need to convert the json to a string, clean it and then convert it back to a json but searching hasn't given me any direction that has helped.
https://stackoverflow.com/questions/67443696/remove-corrupt-start-of-json-in-python May 08, 2021 at 11:03AM
没有评论:
发表评论