Python - 使用 beautifulSoup 查找文本,然后替换原始汤变量 [英] Python - Find text using beautifulSoup then replace in original soup variable
问题描述
commentary = soup.find('div', {'id' : 'live-text-commentary-wrapper'})
findtoure = commentary.find(text = re.compile('Gnegneri Toure Yaya')).replace('Gnegneri Toure Yaya', 'Yaya Toure')
评论包含 Gnegneri Toure Yaya 需要更改为 Yaya Toure 的各种实例.
Commentary contains various instances of Gnegneri Toure Yaya that need changing to Yaya Toure.
findAll()
不起作用,因为 findtoure 是一个列表.
findAll()
doesn't work as findtoure is a list.
我遇到的另一个问题是这段代码只是简单地找到它们并将它们替换为一个名为 findtoure 的新变量,我需要在原始汤中替换它们.
The other problem I have is this code simply finds them and replaces them into a new variable called findtoure, I need to replace them in the original soup.
我想我只是从错误的角度看待这个问题.
I think I am just looking at this from the wrong perspective.
推荐答案
只是 .replace()
你不能做你想做的事.来自关于 NavigableString
的 BeautifulSoup 文档:
You cannot do what you want with just .replace()
. From the BeautifulSoup documentation on NavigableString
:
您不能就地编辑字符串,但可以使用 replace_with()
.
You can’t edit a string in place, but you can replace one string with another, using
replace_with()
.
这正是你需要做的;获取每个匹配项,然后在包含的文本上调用 .replace()
并将原始文本替换为:
That's exactly what you need to do; take each match, then call .replace()
on the contained text and replace the original with that:
findtoure = commentary.find_all(text = re.compile('Gnegneri Toure Yaya'))
for comment in findtoure:
fixed_text = comment.replace('Gnegneri Toure Yaya', 'Yaya Toure')
comment.replace_with(fixed_text)
如果您想进一步使用这些评论,则需要进行新的查找:
If you want to use these comments further, you'll need to do a new find:
findtoure = commentary.find_all(text = re.compile('Yaya Toure'))
或者,如果您只需要结果 strings(所以 Python str
对象,而不是 NavigableString
对象仍然连接到 BeautifulSoup
对象),只需收集 fixed_text
对象:
or, if you all you need is the resulting strings (so Python str
objects, not NavigableString
objects still connected to the BeautifulSoup
object), just collect the fixed_text
objects:
findtoure = commentary.find_all(text = re.compile('Gnegneri Toure Yaya'))
fixed_comments = []
for comment in findtoure:
fixed_text = comment.replace('Gnegneri Toure Yaya', 'Yaya Toure')
comment.replace_with(fixed_text)
fixed_comments.append(fixed_text)
这篇关于Python - 使用 beautifulSoup 查找文本,然后替换原始汤变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!