我正在嘗試從以下 url 下載所有 csv 檔案:https ://emi.ea.govt.nz/Wholesale/Datasets/FinalPricing/EnergyPrices ,但不幸的是我無法按預期成功。這是我的嘗試:
soup = BeautifulSoup(page.content, "html.parser")
market_dataset = soup.findAll("table",{"class":"table table-striped table-condensed table-clean"})
for a in market_dataset.find_all('a', href=True):
print("Found the URL:", a['href'])
誰能幫幫我嗎。如何獲取 csv 檔案的所有 url。
uj5u.com熱心網友回復:
選擇更具體的元素,例如 withcss selectors并注意您必須連接hrefwith baseUrl:
['https://emi.ea.govt.nz' a['href'] for a in soup.select('td.csv a')]
或者只是更改您的代碼并使用find()而不是findAll()查找表,這會導致以下屬性錯誤:
AttributeError:ResultSet 物件沒有屬性“find_all”。您可能將元素串列視為單個元素。當您打算呼叫 find() 時,您是否呼叫了 find_all()?
market_dataset = soup.find("table",{"class":"table table-striped table-condensed table-clean"})
注意: 在新代碼中使用嚴格find_all()而不是舊語法findAll()或兩者的混合。
例子
from bs4 import BeautifulSoup
import requests
url = 'https://emi.ea.govt.nz/Wholesale/Datasets/FinalPricing/EnergyPrices'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
['https://emi.ea.govt.nz' a['href'] for a in soup.select('td.csv a')]
輸出
['https://emi.ea.govt.nz/Wholesale/Datasets/FinalPricing/EnergyPrices/20220318_FinalEnergyPrices_I.csv',
'https://emi.ea.govt.nz/Wholesale/Datasets/FinalPricing/EnergyPrices/20220317_FinalEnergyPrices_I.csv',
'https://emi.ea.govt.nz/Wholesale/Datasets/FinalPricing/EnergyPrices/20220316_FinalEnergyPrices.csv',
'https://emi.ea.govt.nz/Wholesale/Datasets/FinalPricing/EnergyPrices/20220315_FinalEnergyPrices.csv',
'https://emi.ea.govt.nz/Wholesale/Datasets/FinalPricing/EnergyPrices/20220314_FinalEnergyPrices.csv',
'https://emi.ea.govt.nz/Wholesale/Datasets/FinalPricing/EnergyPrices/20220313_FinalEnergyPrices.csv',
'https://emi.ea.govt.nz/Wholesale/Datasets/FinalPricing/EnergyPrices/20220312_FinalEnergyPrices.csv',...]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/448288.html
上一篇:從div內的網站中刮取一行文本
下一篇:無法從HTML代碼中洗掉價格
