【发布时间】:2020-07-27 05:23:51
【问题描述】:
我有一个文件,其中包含指向不同页面的链接。我想用id="links" 将它们插入到div 下的HTML 文件中。需要明确的是,div 已经存在,所以我不想在任何地方创建新标签。
显示了我的 python 和 HTML 尝试
<html>
<head>
</head>
<body>
<div id="links">
</div>
</body>
</html>
from bs4 import BeautifulSoup
soup = BeautifulSoup(open('myhtml.html'),'html.parser')
div = soup.select("#links")
print(div)
content = '<a href="abcd.com">Link</a>'
div.append(BeautifulSoup(content,'html.parser'))
print(div)
print (soup)
注意-我看过以下页面,但它们没有解决我的问题 Insert HTML into an element with BeautifulSoup Append markup string to a tag in BeautifulSoup Edit and create HTML file using Python Using BeautifulSoup to modify HTML Get contents by class names using Beautiful Soup
【问题讨论】:
标签: python html beautifulsoup