Search code examples
jsonpandasrequest

Json to pandas dataframde


I would like to get the list of messages from this page https://stocktwits.com/symbol/SPY they provide a free api where I can get json file https://api.stocktwits.com/api/2/streams/symbol/SPY.json.

import requests
import json

request=requests.get('https://api.stocktwits.com/api/2/streams/symbol/SPY.json')
json_data=request.json()

I used the code above, but I keep getting error JSONDecodeError: Expecting value: line 1 column 1 (char 0) I would greatly appriceate it if someone could help me get the list of messages in pandas dataframe form


Solution

  • It seems like you need to pass the User-Agent header to get the expected response.

    >>> import requests
    >>> request = requests.get('YOUR_URL', headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'})
    

    Once the request is successful you can use pandas.read_json API to construct the dataframe from JSON returned from the server.