【问题标题】:Beautiful Soup, searching by content of tag with tag in itBeautiful Soup,按标签内容搜索,里面有标签
【发布时间】:2014-07-11 21:30:47
【问题描述】:
<th rowspan="3" style="background:#c0cfe4; width:7em">present</th>
<td>ich <a href="/wiki/mache" title="mache">mache</a></td>
<td>wir <strong class="selflink">machen</strong></td>
<th rowspan="3" style="background:#c0cfe4; width:7em">i</th>
<td>ich <a href="/wiki/mache" title="mache">mache</a></td>
<td>wir <strong class="selflink">machen</strong></td>
</tr>
<tr>
<td>du <a href="/wiki/machst" title="machst">machst</a></td>
<td>ihr <a href="/wiki/macht" title="macht">macht</a></td>
<td>du <a href="/wiki/machest" title="machest">machest</a></td>
<td>ihr <a href="/wiki/machet" title="machet">machet</a></td>
</tr>
<th colspan="6" style="background:#9999DF">future i</th>
</tr>
<tr>
<th rowspan="3" style="background:#ccccff">infinitive</th>
<td rowspan="3" colspan="2">machen werden</td>
<th rowspan="3" style="background:#ccccff">subjunctive i</th>
<td>ich werde machen</td>
<td>wir werden machen</td>
</tr>
<tr>
<td>du werdest machen</td>
<td>ihr werdet machen</td>
</tr>
<tr>
<td>er werde machen</td>
<td>sie werden machen</td>
</tr>

我试图在第 9 行提取 &lt;td&gt;du &lt;a href="/wiki/machst" title="machst"&gt;machst&lt;/a&gt;&lt;/td&gt;。当我使用 soup.find_all("td" text="re.compile("^du)) 执行搜索时,我得到的只是第 24 行的标签。这样做的正确方法是什么?

【问题讨论】:

  • 我认为这不会影响问题,但我注意到第 7 行上的第一个 &lt;/tr&gt; 结束标记缺少相应的 &lt;tr&gt; 开始标记。我假设您只是在创建问题时错过了复制标签。

标签: python html html-parsing beautifulsoup


【解决方案1】:

作为一种替代方法,您可以得到 next td,它的 textdu 开头:

print next(td for td in soup.find_all("td") if td.text.startswith('du')) 

另外,您可以pass a functionfind_all()

def td_with_du(tag):
    return tag.name == 'td' and tag.text.startswith('du')

print soup.find_all(td_with_du)

演示:

>>> from bs4 import BeautifulSoup
>>> data = """
Your HTML code goes here
"""
>>> soup = BeautifulSoup(data)
>>> def td_with_du(tag):
...     return tag.name == 'td' and tag.text.startswith('du')
... 
>>> for td in soup.find_all(td_with_du):
...     print td.text
... 
du machst
du machest
du werdest machen

【讨论】:

    【解决方案2】:

    问题是你不能同时匹配带有文本和嵌套标签的标签(参见How can I get text out of a <dt> tag with a <span> inside?),这就是为什么你唯一的匹配是&lt;td&gt;du werdest machen&lt;/td&gt;

    当标签本身包含嵌套标签时,标签对象的string 属性是None。然而,正如 Martijn Pieters 在上面的链接中所说,.text 包含所有嵌套标签组合中的所有字符串,这就是为什么

    >>> a = soup.find_all('td')[0]
    >>> a
    <td>ich <a href="/wiki/mache" title="mache">mache</a></td>
    >>> print(a.string)
    None
    >>> print(a.text)
    ich mache
    >>> b = soup.find_all('td', text=re.compile('^du'))[0]
    >>> b
    <td>du werdest machen</td>
    >>> print(b.string)
    du werdest machen
    >>> print(b.text)
    du werdest machen
    

    有关解决此问题的方法,您可以查看 alecxe 的答案。

    【讨论】:

      【解决方案3】:

      此解决方案假定您不限于过滤text="re.compile("^du)

      虽然有几行以“du”开头的文本,但您的数据中只有一行包含href="/wiki/machst"。所以,如果你过滤 href 属性,你会得到“a”标签,如果你取它的父级,你会得到你想要的“td”标签:

      soup.find(href="/wiki/machst").parent
      

      如果你需要使用find_all,而不是find

      for a in soup.find_all(href="/wiki/machst"):
          print a.parent
      

      如果由于某种原因无法使用此解决方案,如果您能阐明您的操作要求和限制,将会很有帮助。

      【讨论】:

        猜你喜欢
        • 2017-02-19
        • 2019-02-20
        • 1970-01-01
        • 1970-01-01
        • 2014-06-11
        • 2021-02-22
        • 2017-12-31
        • 2020-08-24
        • 1970-01-01
        相关资源
        最近更新 更多