如何data-amount使用 BeautifulSoup獲取 JSON 資料?

這是我嘗試過的:
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)
uj5u.com熱心網友回復:
您可以通過使用 訪問data-amount 屬性來訪問 JSON 資料[<attribute-name>]。在你的例子中:
res = row.find('span', attrs={'class':'price-data'})["data-amount"]
此外,您可以將 JSON 資料(變數res)轉換為字典(dict),您可以在其中使用內置模塊訪問key/ :valuesjson
import json
...
res = json.loads(row.find('span', attrs={'class':'price-data'})["data-amount"])
print(res) # `<class 'dict'>`
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/310938.html
上一篇:用另一個串列遍歷一個串列
