python - 如何将url添加到html文件中
问题描述
<html>
<body>
<h2>HTML Iframes</h2>
<p>You can use the height and width attributes to specify the size of the iframe:</p>
<iframe src="https://www.google.com/maps/embed/v1/place?key=AIzaSyAPi0wzs7IlNc4nlL3atU7iCd-A9QXfuHs&q=4.5596%2C-76.2801&zoom=18&maptype=satellite" height="200" width="300"></iframe>
</body>
</html>
因此,在我的计算机上创建了这个 html 文件,现在我需要将 src 之后的内容更改为新字符串,例如 -
那么如何在 src= 之后将该示例行添加到 html 文件中?
解决方案
from bs4 import BeautifulSoup
htmlstr = '''
<html>
<body>
<h2>HTML Iframes</h2>
<p>You can use the height and width attributes to specify the size of the iframe:</p>
<iframe src="https://www.google.com/maps/embed/v1/place?key=AIzaSyAPi0wzs7IlNc4nlL3atU7iCd-A9QXfuHs&q=4.5596%2C-76.2801&zoom=18&maptype=satellite" height="200" width="300"></iframe>
</body>
</html>
'''
soup = BeautifulSoup(htmlstr)
iframe = soup.find('iframe')
iframe["src"] = "test"
print(soup)
这应该是您正在寻找的解决方案。将 替换为iframe["src"] = "test"
您要提供的链接并将结果保存回 html 文件。
推荐阅读
- visual-studio - 如何在 Visual Studio 工具栏中添加 WebView2 控件?
- c - 如何返回并打印出第一个字符串(string1)的起始地址?
- r - R - 使用日期重叠的意外结果
- ios - 在 Swift 上运行应用程序时意外发现“nil”
- flutter - Dart 是否有相当于 TypeScript 的 --noImplicitAny,强制静态类型,除非明确禁用?
- c# - 将旧的 Switch() 语法转换为新的 c# 8
- angular - 以角度暂停后如何显示视频海报?
- spring-boot - 使用 jwt 进行身份验证如何验证 http 请求
- makefile - 有没有更好的方法来构建 Makefile 项目中的一个 cpp?
- php - 如何使用 mysqli_fetch_array() 在关联数组中插入值