靓汤:获取搜索结果标签的内容

2024-04-25 14:18:48 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试使用beautiful soup(它是一个“tag”对象)获取这种类型的html片段的内容。你知道吗

<span class="font5"> arrives at this calculation from the Torah’s report that the deluge (rains) began on the 17<sup>th</sup> day of the second month </span>

我试过:

soup.contents.find_all('span')
soup.find_all('span')
soup.find_all(re.compile("font[0-9]+"))
soup.string
soup.child

而这些似乎都不起作用。我能做什么?你知道吗


Tags: the对象类型内容htmltagallfind
3条回答

soup.find_all('span')有效;返回所有span标记。你知道吗

如果要获取带有font<N>类的span标记,请将模式指定为关键字参数class_

soup.find_all('span', class_=re.compile('font[0-9]+'))
print ''.join(soup.findAll(text=True))

(回答here

如果以font开头足够独特,您还可以使用css选择器查找以font开头的类:

soup.select("span[class^=font]")

相关问题 更多 >