gpt4 book ai didi

python - 如何使用 python 和 BeautifulSoup 添加 html 属性

转载 作者:行者123 更新时间:2023-12-01 05:50:14 24 4
gpt4 key购买 nike

html_doc="""
<html>
<head></head>
<body>
<ul class="navigation"></ul>
</body>
</html>
"""
link_doc="""
<p>
<li><a href="http://example.com/elsie" id="link1">Elsie</a></li>
<li><a href="http://example.com/lacie" id="link2">Lacie</a></li>
<li><a href="http://example.com/tillie" id="link3">Tillie</a></li>
</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
link = BeautifulSoup(link_doc)

navigation = soup.find_all("ul", {"class" : "navigation"})
links = link.find_all('li')

for i in range(0,3): # assume I would like to write to 3 files
for n in range(len(links)):
navigation[0].append(links[n])
output = navigation[0].prettify(formatter=None)
file = open(str(i) + '.html', 'w')
file.write(output)
file.close()
navigation[0].clear()

我有两个这样的简单文档。我想做的是在 <ul class="navigation"> 之间添加一些链接并将它们写入3个文件。

但我也想添加类属性 active<li>元素取决于文件顺序。例如 0.html 应该是这样的:

<ul class="navigation">
<li class="active">
<a href="http://example.com/elsie" id="link1">
Elsie
</a>
</li>
<li>
<a href="http://example.com/lacie" id="link2">
Lacie
</a>
</li>
<li>
<a href="http://example.com/tillie" id="link3">
Tillie
</a>
</li>
</ul>

1.html 会像这样等等

<ul class="navigation">
<li>
<a href="http://example.com/elsie" id="link1">
Elsie
</a>
</li>
<li class="active">
<a href="http://example.com/lacie" id="link2">
Lacie
</a>
</li>
<li>
<a href="http://example.com/tillie" id="link3">
Tillie
</a>
</li>
</ul>

我怎样才能做到这一点?

最佳答案

编辑:我添加了一种更好的插入链接的方法。我希望这有帮助!

编辑 x2:现在应该可以工作:

添加链接:

from bs4 import BeautifulSoup
html = '<ul class="navigation"> foo <i>hi</i> bar</ul>'
soup = BeautifulSoup(html)
original_tag = soup.ul
new_tag = soup.new_tag('a', href='http://www.example.com')
new_tag.insert(0, 'Text!')
original_tag.append(new_tag)
print original_tag
# Prints:
# <ul class="navigation"> foo <i>hi</i> bar<a href="http://www.example.com">Text!</a></ul>
# If you don't want it to go to the last bit of the tag all the time,
# use original_tag.insert(). Here's a link to the documentation about it:
# http://www.crummy.com/software/BeautifulSoup/bs4/doc/#insert

要添加 class="active",请将最后一个 for 循环替换为:

for i in range(0,3): # assume I would like to write to 3 files
for n in range(len(links)):
navigation[0].append(links[n])
output = navigation[0].prettify(formatter=None)
soup2 = BeautifulSoup(output)
mylist = soup2.find_all('li')
mylist[i]['class'] = 'active'
filee = open(str(i) + '.html', 'w') # I changed it to filee to avoid confusion with the file function
filee.write(str(soup2))
filee.close()
navigation[0].clear()

关于python - 如何使用 python 和 BeautifulSoup 添加 html 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14561583/

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