本文介绍了使用 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?
推荐答案
这是一个清晰的 BeautifulSoup 方式,当第二个 h1
标签是第一个的兄弟时:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!