用Beautiful Soup添加HTML属性
我有一个简单的需求:我想用Beautiful Soup在HTML部分插入一个属性,并且这个属性能在网页上显示出来。
比如,对于以下的HTML代码:
<input type="submit" name="name1">
我有这样的BeautifulSoup代码:
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('<input type="submit" name="name1">')
getElementByName = soup.find(attrs={'name':'name1'})
# getElementByName.insert method does not reflect the insertion on the webpage
我想用Beautiful Soup实现类似下面这个JavaScript的方法:
document.getElementByName("name1").setAttribute("id", "id1");
我觉得Beautiful Soup可能做不到这一点,因为它只是一个解析XML/HTML的工具。不过我还是想问问,如果有人知道答案的话?
谢谢,
Amit