gpt4 book ai didi

python - 如何使用 BeautifulSoup 将标签内容从一个 soup 移动到模板 soup

转载 作者:太空宇宙 更新时间:2023-11-03 18:33:26 28 4
gpt4 key购买 nike

我有一个这样的模板:

 <html><body><div id="here"></div></body></html>

和这样的输入 HTML

 <html><body>COMPLEX HTML</body></html>

其中 COMPLEX_HTML 是很多子标签(很干净 - 验证)

我试图将输入 HTML 的 body 标记内的 HTML 移动到模板中的 #here div 中,以获取此内容

 <html><body><div id="here">COMPLEX HTML</div></body></html>

我尝试过:

t = BeautifulSoup("<html><body><div id=\"here\"></div></body></html>")
pc = t.find("div", id="here")
s = BeautifulSoup(open("complex.html"))

# this prints every tag in body
for b in s.body.contents:
print b.name

# this prints only some of the tags
for b in s.body.contents:
print b.name
pc.append(b)

pcs.body 中的所有其他标记结束

就好像附加 b 使迭代器向前移动。如何从一种汤中获取 HTML 结构并将其放入另一种汤中?

最佳答案

你可以这样做:

from bs4 import BeautifulSoup

html = """<html><body><div id="here"></div></body></html>"""

soup = BeautifulSoup(html)
div = soup.find("div", id="here")

html2 = """<html><body><script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//cdn.sstatic.net/Js/stub.en.js?v=283ea58c715b"></script>
<link rel="stylesheet" type="text/css" href="//cdn.ss tatic.net/stackoverflow/all.css? v=71d362e7c10c">
</body></html>"""

soup1 = BeautifulSoup(html2)
value = soup1.body.extract()

div.append(value)
print div

输出是:

<div id="here"><body><script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//cdn.sstatic.net/Js/stub.en.js?v=283ea58c715b"></script>
<link href="//cdn.sstatic.net/stackoverflow/all.css?v=71d362e7c10c" rel="stylesheet" type="text/css">
</link></body></div>

如果您想要body中的内容,您可以这样做:

#the above same lines

soup1 = BeautifulSoup(html2)
value = soup1.body.extract()

div.append(value)
# replaces a tag with whatever’s inside that tag.
div.body.unwrap()
print div

输出是:

<div id="here"><script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//cdn.sstatic.net/Js/stub.en.js?v=283ea58c715b"></script>
<link href="//cdn.sstatic.net/stackoverflow/all.css?v=71d362e7c10c" rel="stylesheet" type="text/css">
</link></div>

关于python - 如何使用 BeautifulSoup 将标签内容从一个 soup 移动到模板 soup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22024409/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com