【发布时间】:2020-03-18 10:49:18
【问题描述】:
以下代码仅返回空括号。我看过这篇帖子Why does bs4 return tags and then an empty list to this find_all() method?,但它有所不同,因为我没有使用find_all(),而是.select()。请注意,我将“nth-child”更改为“nth-of-type”以避免错误。
import bs4
import requests
res = requests.get('http://www.sharkresearchcommittee.com/pacific_coast_shark_news.htm')
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
soup.select('body > div > div:nth-of-type(2) > center > table > tbody > tr:nth-of-type(1) >td:nth-of-type(2) > p:nth-of-type(8) > strong:nth-of-type(1) > font')
输出为 [ ]
【问题讨论】:
-
...因为有 0 个匹配项?
-
请注意,
tbody元素实际上并不存在于标记中,它们是由浏览器和解析器添加的,例如html5lib(与html.parser不同)。 -
@Blender - 感谢您的回复。我取出了 tbody 仍然得到相同的输出
标签: python beautifulsoup jupyter-notebook