Web-scraping with requests python library- Key error

Viewed 50

Can you sort this code out please?

import requests
import pandas as pd

url = "https://www.cse.lk/api/aspi"

data = requests.post(url).json()
df = pd.DataFrame(data["reqAspi"])

print(df)

This says there is a KeyError: 'reqAspi'

Can anyone help me to find the key for this api link?

2 Answers

First go to chrome developer mode and refresh your site copy paste company name and see from image you can uderstand very well. (In Left hand side you can see!)

Image: enter image description here

You have selected wrong key from json data it should be main_data['reqASPIIndices'] this.

import requests
import pandas as pd

url = "https://www.cse.lk/api/aspi"

req= requests.post(url)
main_data=req.json()
data=main_data['reqASPIIndices']
df=pd.DataFrame(data)

Output:

id  name    symbol  lastTradedTime  price   turnover    sharevolume tradevolume change  changePercentage
0   328 CIC HOLDINGS PLC    CIC.X0000   1.628845e+12    39.50   939905.2    23661.0 50.0    0.4 1.023018
1   357 SMB LEASING PLC SEMB.X0000  1.628845e+12    0.20 1380198.4  6895960.0   128.0   0.0 0.000000
.....

To solve KeyError, the response is returning the data as

{'reqASPIIndices': [{'id': 328, 'name': 'CIC HOLDINGS PLC', 'symbol': 'CIC.X0000', 'lastTradedTime': 1 ....

So you need to use key - 'reqASPIIndices'

import requests
import pandas as pd

url = "https://www.cse.lk/api/aspi"

data = requests.post(url).json()
df = pd.DataFrame(data["reqASPIIndices"])

print(df)
Related