How can I get the JSON data under data-amount
using BeautifulSoup?
This is what I have tried:
import requests
from bs4 import BeautifulSoup
url = "https://azure.microsoft.com/en-au/pricing/details/managed-disks/"
response = requests.get(url)
table_doc = BeautifulSoup(response.text, "html.parser")
data = []
table = table_doc.find_all(
"table", attrs={"class": "data-table__table data-table__table--pricing"}
)[0]
table_body = table.find("tbody")
rows = table_body.find_all("tr")
for row in rows:
cols = row.find_all("td")
res = row.find("span", attrs={"class": "price-data"})
print(res)
CodePudding user response:
You can access the JSON data by accessing the data-amount
attribute using [<attribute-name>]
. In your example:
res = row.find('span', attrs={'class':'price-data'})["data-amount"]
Additionally, you can convert the JSON data (the variable res
) to a dictionary (dict
) where you can acess the key
/values
using the built-in json
module:
import json
...
res = json.loads(row.find('span', attrs={'class':'price-data'})["data-amount"])
print(res) # `<class 'dict'>`