簡體   English   中英

Python 網頁抓取與美麗的湯

[英]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.tbodyNone

您可以使用以下命令直接從表中獲取所有行:

table = soup.find("table", attrs={"class": "tableStyle"})
table_data = table.findAll('tr')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM