python - 使用 BeautifulSoup 抓取特定的表行

标签 python html selenium beautifulsoup

我尝试从我的 base_url 页面中抓取特定行(图中标记为蓝色圆圈)。页面源代码在另一张图片中。

我的目标是获取这些 标签,但不知何故我无法用我的代码获取它们。

我的代码:

from bs4 import BeautifulSoup
from selenium import webdriver
import requests, csv, re, pandas, numpy

base_url = "http://www.basket.fi/sarjat/ottelu/?game_id=3502579&season_id=93783&league_id=4+"+"#mbt:2-400$t&0=1"
browser = webdriver.PhantomJS()
browser.get(base_url)
table = BeautifulSoup(browser.page_source, 'lxml')

for data in table.find_all("tr",{"class":"row2"}):
    print(data.find("td").text)

enter image description here

enter image description here

最佳答案

通常您可以通过属性选择 html 元素,但对于本文档,“class”属性并不是很有帮助,因为同一类中还有许多其他“tr”标签。

在这种情况下,您可以使用列表索引来选择标签。

for td in table.find_all("tr", {"class":"row2"})[25].find_all('td')[1:]:
    print(td.get_text(strip=True))

关于python - 使用 BeautifulSoup 抓取特定的表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46222995/

相关文章:

Python pandas 没有属性 ols - 错误(滚动 OLS)

java - 使用 JSoup 获取表的数据代码值

java - 迭代 LinkedHashMap 并在新行上返回每个 ‘String’ 元素

python - 如何找到并单击带有 Selenium 的隐藏按钮?

python - ExpectedCondition.invisibility_of_element_located 需要更多时间(selenium web driver-python)

Python raw_input 使用 TAB 而不是 ENTER?

python - Flask 和 SQLAlchemy 的 "unexpected EOF on client connection"

python - 简单的 Python UDP 服务器 : trouble receiving packets from clients other than localhost

html - Bootstrap - 文本字段的边框颜色

javascript - 如何在 CSS/JS 中创建固定/粘性侧边栏?