简体   繁体   English

如何使用BeautifulSoup获取内容之间 <hr class = 'calibre2'> ... <hr class=“calibre2” />

[英]How to use BeautifulSoup to get content between<hr class = 'calibre2'> … <hr class=“calibre2” />

<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" />

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>之间获取内容,因为hr不是close标签,所以我不能使用一些简单的方法,我想如果可以使用find_next_elements,但是当他看到<hr class = 'calibre2'> ,如何让他停下来,所以我可以得到那些内容,谢谢。

You can loop over all hr elements and use .find_next_siblings() to iterate over the next sibling elements. 您可以遍历所有hr元素,并使用.find_next_siblings()迭代下一个同级元素。 Then, if you meet hr , break the loop: 然后,如果遇到hr ,请中断循环:

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

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

You can check for the hr and calibre2 class in conjunction with find_all_next https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-all-next-and-find-next 您可以与find_all_next https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-all-next-and-find-next一起检查hr和calibre2类

from bs4 import BeautifulSoup

testStr = """
<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" />
""";
soup = BeautifulSoup(testStr, 'lxml')
hrTag = soup.hr

nextTags = hrTag.find_all_next()

content = []

for item in nextTags:
    # check if we have reached the second calibre2 hr
    print("Name %s ; Class %s" % (item.name, item['class'][0]))
    if item.name == 'hr' and item['class'][0] == 'calibre2':
        break
    content.append(item)
print(content)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM