首页 > 解决方案 > 如何将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 之后的内容更改为新字符串,例如 -

https://www.google.com/maps/embed/v1/place?key=AIzaSyAPi0wzs7IlNc4nlL3atU7iCd-A9QXfuHs&q=25.5596%2C-7.2801&zoom=18&maptype=satellite

那么如何在 src= 之后将该示例行添加到 html 文件中?

标签: pythonhtmliframe

解决方案


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 文件。


推荐阅读