【发布时间】:2015-02-16 16:30:53
【问题描述】:
我希望在 BeautifulSoup 中使用 findParent() 方法来查找具有 id 属性的特定标签的父级。例如,考虑以下示例 XML:
<monograph>
<section id="1234">
<head>Test Heading</head>
<p>Here's a paragraph with some text in it.</p>
</section>
</monograph>
假设我已经匹配了段落中的某些内容,我想使用 findParent 不加选择地在树上找到具有 id 属性的第一个父级。比如:
for hit in monograph(text="paragraph with"):
containername = hit.findParent(re.compile([A-Za-z]+), {id}).name
但是,前面的代码没有返回任何命中。
【问题讨论】:
标签: python python-3.x beautifulsoup