python - 如何在 BS4 中添加嵌套的 DIV
问题描述
我想要的只是在我的 HTML 文件中的“容器 DIV”中添加一个 DIV,但 BS4 在该标签之后或之前一直在做!我无法让它进入容器。
这是我的 HTML
<body>
<div class="container">
<div class="bg1"><img src="1.png"></div>
<div class="bg2"><img src="2.png"></div>
</div>
</body>
这是我的 Python:
with open("/var/www/html/index.html") as inf:
html = inf.read()
soup = bs4.BeautifulSoup(html, features="html.parser")
prettyHTML = soup.prettify() #prettify the html
children = soup.div.container
addMyDiv= soup.new_tag('div', id='NewDIV') # create the DIV
soup.html.body.insert(1, addMyDiv)
with open("/var/www/html/index.html", "w") as outf:
outf.write(str(prettyHTML))
它只是在“容器”之后不断添加 DIV 我如何强制它添加到 CONTAINER 内部?
解决方案
推荐阅读
- reactjs - TypeScript React:使用 useRef 获取表单值
- firebase - 在没有身份验证的情况下调用 Firebase 远程配置 REST API
- python - 为什么python中的IF会输出错误?
- printing - MHT文件打印需要时间
- azure - Azure“Kudu”中的 .SVC 文件为空
- r - R函数'stl'。如何制作季节性?
- sql - SQL - 将条件应用于具有相似名称的多个列
- amazon-sagemaker - 在 sagemaker Mxnet 1.4.1 映像中安装用于推理的外部库的最佳方法是什么?
- asp.net-mvc - 在 ASP.NET 中使用带有 Ajax.BeginForm 的 Javascript Bootbox
- php - PHP : 扫描手机二维码,打开链接