【问题标题】:Scraping specific table row with BeautifulSoup用 BeautifulSoup 抓取特定的表行
【发布时间】:2018-02-23 16:01:22
【问题描述】:

我尝试从我的 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)

【问题讨论】:

  • 这是什么问题,请解释一下?
  • @TarunLalwani 我的代码没有得到我想要的 标签。
  • 是否可以在 pastebin 或原始 url 中获取 HTML?

标签: python html selenium beautifulsoup


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 2022-01-10
    • 1970-01-01
    • 2016-07-22
    • 2018-08-26
    • 1970-01-01
    相关资源
    最近更新 更多