【发布时间】:2021-08-23 20:51:17
【问题描述】:
有很多类似的问题,但没有人回答我的问题。我正在尝试使用 CSS 选择器在美丽的汤中找到标签。
The specific section of html I am trying to scrape, as the full html is quite large
我正在抓取的网址在我的代码中。
这里有一些测试代码,希望能显示我的问题:
url = "https://www.basketball-reference.com/boxscores/201510310MEM.html"
response = urlopen(url)
html = response.read().decode()
# proves the element I am selecting exists in the html
print(html.find("table class=\"suppress_all stats_table\" id=\"four_factors\" data-cols-to-freeze=\",1\""))
soup = BeautifulSoup(html, 'html.parser')
# this line prints a similar piece of data to the one I want, but not correct
print(soup.select('tbody > tr > td[data-stat="off_rtg"]')[0].get_text())
# when I try being more specific, it prints an empty list
print(soup.select('table[id="four_factors"] tbody > tr > td[data-stat="off_rtg"]'))
输出:
78720
98
[]
正如我的代码所示,可以使用 python 的 String.find() 方法找到的元素由于某种原因对 BeautifulSoup 是不可见的。我尝试使用 BeautifulSoup.find() 和 .findAll() 而不是具有相同结果的 css 选择器。我试过使用 lxml 解析器,结果相同。
【问题讨论】:
-
请提供您正在抓取的 html 的链接(例如使用 pastebin)而不是图片,这样可以更轻松地进行测试:)
-
我可以试试,但是如果您需要完整的 html,您不能直接转到代码中的链接,右键单击,然后选择检查吗? @Seon
-
我的错,我读得很快,错过了链接。
标签: python html css beautifulsoup