[英]Python Web Scraping with Beautiful Soup
我正在嘗試從下面的站點中提取整個表格並將其存儲為數據框,但是在嘗試提取所有標題時遇到錯誤。 該表似乎具有這些屬性,因此不確定為什么會發生這種情況。
URL = "http://www.ercot.com/content/cdr/html/real_time_spp"
page = requests.get(URL).text
soup = BeautifulSoup(page, "lxml")
table = soup.find("table", attrs={"class": "tableStyle"})
table_data = table.tbody.find_all("tr")
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-241-362ee5fb0444> in <module>
1 table = soup.find("table", attrs={"class": "tableStyle"})
----> 2 table_data = table.tbody.find_all("tr")
AttributeError: 'NoneType' object has no attribute 'find_all'
該頁面的 HTML 沒有tbody
元素,這就是table.tbody
為None
。
您可以使用以下命令直接從表中獲取所有行:
table = soup.find("table", attrs={"class": "tableStyle"})
table_data = table.findAll('tr')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.