如何从 HTML table in python 中的特定单元格获取数据?
How can I get data from a specific cell in an HTML table in python?
This link contains the table I'm trying to parse.
我正在尝试在 Python 中使用 BeautifulSoup
。我是 BeautifulSoup
和 HTML 的新手。这是我解决问题的尝试。
soup = BeautifulSoup(open('BBS_student_grads.php'))
data = []
table = soup.find('table')
rows = table.find_all('tr') #array of rows in table
for x,row in enumerate(rows[1:]):# skips first row
cols = row.find_all('td') # finds all cols in rows
for y,col in enumerate(cols): # iterates through col
data.append([])
data[x].append(col) # puts table into a 2d array called data
print(data[0][0]) #prints top left corner
我正在尝试提取 table 中的所有名称,然后更新列表中的名称,然后更新 table。我也在使用这个 HTML 的本地副本。临时修复,直到我学会如何做更多的网络编程。
非常感谢帮助
我认为您只需要 tr
元素中的 td
元素和 class="searchbox_black"
。
您可以使用 CSS Selectors
获取所需的 td
元素:
for cell in soup.select('tr.searchbox_black td'):
print cell.text
它打印:
BB Salsa
Adams State University Alamosa, CO
Sensei: Oneyda Maestas
Raymond Breitstein
...
This link contains the table I'm trying to parse.
我正在尝试在 Python 中使用 BeautifulSoup
。我是 BeautifulSoup
和 HTML 的新手。这是我解决问题的尝试。
soup = BeautifulSoup(open('BBS_student_grads.php'))
data = []
table = soup.find('table')
rows = table.find_all('tr') #array of rows in table
for x,row in enumerate(rows[1:]):# skips first row
cols = row.find_all('td') # finds all cols in rows
for y,col in enumerate(cols): # iterates through col
data.append([])
data[x].append(col) # puts table into a 2d array called data
print(data[0][0]) #prints top left corner
我正在尝试提取 table 中的所有名称,然后更新列表中的名称,然后更新 table。我也在使用这个 HTML 的本地副本。临时修复,直到我学会如何做更多的网络编程。
非常感谢帮助
我认为您只需要 tr
元素中的 td
元素和 class="searchbox_black"
。
您可以使用 CSS Selectors
获取所需的 td
元素:
for cell in soup.select('tr.searchbox_black td'):
print cell.text
它打印:
BB Salsa
Adams State University Alamosa, CO
Sensei: Oneyda Maestas
Raymond Breitstein
...