如何将URL添加到HTML文件

时间:2019-03-29 16:32:32

标签: python html iframe

<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文件中?

1 个答案:

答案 0 :(得分:0)

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