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
我试图提取表中的所有名称,然后更新列表中的名称,然后更新表。我还使用此 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
...
关于python - 如何从 python 中的 HTML 表格中的特定单元格获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28911361/