使用beautifulsoup python更改内部标记的文本

时间:2017-10-30 22:01:57

标签: python beautifulsoup

我想更改使用inner text获取的HTML中的代码的Beautifulsoup

示例:

<a href="index.html" id="websiteName">Foo</a>

变成:

<a href="index.html" id="websiteName">Bar</a>

我已设法通过它获取标签:

HTMLDocument.find(id='websiteName')

但我无法更改代码的inner text

print HTMLDocument.find(id='websiteName')

a = HTMLDocument.find(id='websiteName')
a = a.replaceWith('<a href="index.html" id="websiteName">Bar</a>')

// I have tried using this as well
a = a.replaceWith('Bar')

print a

输出:

<a href="index.html" id="websiteName">Foo</a>
<a href="index.html" id="websiteName">Foo</a>

1 个答案:

答案 0 :(得分:2)

尝试更改字符串元素:

from bs4 import BeautifulSoup as soup

html = """
<a href="index.html" id="websiteName">Foo</a>
"""
soup = soup(html, 'lxml')
result = soup.find(id='websiteName')

print(result)
# >>> <a href="index.html" id="websiteName">Foo</a>

result.string.replace_with('Bar')
print(result)
# >>> <a href="index.html" id="websiteName">Bar</a>
pyplot