如何使用BeautifulSoup在<hr class ='calibre2'&gt;之间获取内容....&lt; hr class ="calibre2"/&gt; [英] How to use BeautifulSoup to get content between&lt;hr class = &#39;calibre2&#39;&gt; ... &lt;hr class=&quot;calibre2&quot; /&gt;

查看:58
本文介绍了如何使用BeautifulSoup在<hr class ='calibre2'&gt;之间获取内容....&lt; hr class ="calibre2"/&gt;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<hr class="calibre2" />
<h3 class="calibre5">-ability</h3> (in nouns 构成名词) : <br class="calibre4" />
<blockquote class="calibre6"><p class="calibre_1"><span class="italic">◊ capability 能力 </span></p></blockquote>

<blockquote class="calibre6"><p class="calibre_1"><span class="italic">◊ responsibility 责任 </span></p></blockquote>

<hr class="calibre2" />
<h3 class="calibre5">-ibility</h3> (in nouns 构成名词) : <br class="calibre4" />
<blockquote class="calibre6"><p class="calibre_1"><span class="italic">◊ capability 能力 </span></p></blockquote>

<blockquote class="calibre6"><p class="calibre_1"><span class="italic">◊ responsibility 责任 </span></p></blockquote>

<hr class="calibre2" />

这是我汤的一部分,我想在两个< hr> 之间获取内容,因为hr不是结束标记,因此我无法使用一些简单的方法,我想我是否可以使用find_next_elements,但是当他看到< hr class ='calibre2'> 时,如何让他停下来,所以我可以得到那些内容,谢谢.

above this is my part of my soup, and I want to get content between the two <hr>, because hr is not a close tag, so I couldn't use some simple method, I have think if I can use find_next_elements, but How can let him stop, when he see <hr class = 'calibre2'>, so I can get those content, thank you.

推荐答案

您可以遍历所有 hr 元素并使用

You can loop over all hr elements and use .find_next_siblings() to iterate over the next sibling elements. Then, if you meet hr, break the loop:

for hr in soup.find_all("hr", class_="calibre2"):
    for item in hr.find_next_siblings():
        if item.name == "hr":
            break

        print(item)
    print("-----")

这篇关于如何使用BeautifulSoup在<hr class ='calibre2'&gt;之间获取内容....&lt; hr class ="calibre2"/&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆