使用BeautifulSoup抓取两个标签之间的所有HTML [英] Using BeautifulSoup to grab all the HTML between two tags

查看:336
本文介绍了使用BeautifulSoup抓取两个标签之间的所有HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些看起来像这样的HTML:

I have some HTML that looks like this:

<h1>Title</h1>

//a random amount of p/uls or tagless text

<h1> Next Title</h1>

我想将所有HTML从第一个h1复制到下一个h1.我怎样才能做到这一点?

I want to copy all of the HTML from the first h1, to the next h1. How can I do this?

推荐答案

当第二个h1标签是第一个的同级标签时,这是清晰的BeautifulSoup方法:

This is the clear BeautifulSoup way, when the second h1 tag is a sibling of the first:

html = u""
for tag in soup.find("h1").next_siblings:
    if tag.name == "h1":
        break
    else:
        html += unicode(tag)

这篇关于使用BeautifulSoup抓取两个标签之间的所有HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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