Ben*_*Ben 5 python beautifulsoup
我正在编写一个 python 脚本,允许将 html 文档转换为reveal.js幻灯片。为此,我需要将多个标签包装在一个<section>标签中。
使用该wrap()方法很容易将单个标签包装在另一个标签中。但是我不知道如何包装多个标签。
一个澄清的例子,原始html:
html_doc = """
<html>
<head>
<title>The Dormouse's story</title>
</head>
<body>
<h1 id="first-paragraph">First paragraph</h1>
<p>Some text...</p>
<p>Another text...</p>
<div>
<a href="http://link.com">Here's a link</a>
</div>
<h1 id="second-paragraph">Second paragraph</h1>
<p>Some text...</p>
<p>Another text...</p>
<script src="lib/.js"></script>
</body>
</html>
"""
"""
Run Code Online (Sandbox Code Playgroud)
我想将<h1>标签和它们的下一个标签包装在<section>标签中,如下所示:
<html>
<head>
<title>The Dormouse's story</title>
</head>
<body>
<section>
<h1 id="first-paragraph">First paragraph</h1>
<p>Some text...</p>
<p>Another text...</p>
<div>
<a href="http://link.com">Here's a link</a>
</div>
</section>
<section>
<h1 id="second-paragraph">Second paragraph</h1>
<p>Some text...</p>
<p>Another text...</p>
</section>
<script src="lib/.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
以下是我进行选择的方式:
from bs4 import BeautifulSoup
import itertools
soup = BeautifulSoup(html_doc)
h1s = soup.find_all('h1')
for el in h1s:
els = [i for i in itertools.takewhile(lambda x: x.name not in [el.name, 'script'], el.next_elements)]
els.insert(0, el)
print(els)
Run Code Online (Sandbox Code Playgroud)
输出:
[<h1 id="first-paragraph">First paragraph</h1>, 'First paragraph', '\n ', <p>Some text...</p>, 'Some text...', '\n ', <p>Another text...</p>, 'Another text...', '\n ', <div><a href="http://link.com">Here's a link</a> </div>, '\n ', <a href="http://link.com">Here's a link</a>, "Here's a link", '\n ', '\n\n ']
[<h1 id="second-paragraph">Second paragraph</h1>, 'Second paragraph', '\n ', <p>Some text...</p>, 'Some text...', '\n ', <p>Another text...</p>, 'Another text...', '\n\n ']
Run Code Online (Sandbox Code Playgroud)
选择是正确的,但我看不到如何将每个选择包装在<section>标签中。
最后我找到了wrap在这种情况下如何使用该方法。我需要了解汤对象中的每一个更改都已就位。
from bs4 import BeautifulSoup
import itertools
soup = BeautifulSoup(html_doc)
# wrap all h1 and next siblings into sections
h1s = soup.find_all('h1')
for el in h1s:
els = [i for i in itertools.takewhile(
lambda x: x.name not in [el.name, 'script'],
el.next_siblings)]
section = soup.new_tag('section')
el.wrap(section)
for tag in els:
section.append(tag)
print(soup.prettify())
Run Code Online (Sandbox Code Playgroud)
这给了我想要的输出。希望这是有帮助的。
| 归档时间: |
|
| 查看次数: |
2394 次 |
| 最近记录: |