gpt4 book ai didi

python - 替换 Beautiful Soup 中的所有智能引号

转载 作者:行者123 更新时间:2023-12-04 03:14:22 25 4
gpt4 key购买 nike

我有一个 HTML 文档,我想用常规引号替换所有智能引号。我试过这个:

for text_element in html.findAll():
content = text_element.string
if content:
new_content = content \
.replace(u"\u2018", "'") \
.replace(u"\u2019", "'") \
.replace(u"\u201c", '"') \
.replace(u"\u201d", '"') \
.replace("e", "x")
text_element.string.replaceWith(new_content)

(使用 e/x 转换只是为了方便查看事情是否正常)

但这是我的输出:
<p>
This amount of investment is producing results: total final consumption in IEA countries is estimated to be
<strong>
60% lowxr
</strong>
today because of energy efficiency improvements over the last four decades. This has had the effect of
<strong>
avoiding morx xnxrgy consumption than thx total final consumption of thx Europxan Union in 2011
</strong>
.
</p>

似乎 BS 正在深入到 child-est 标签,但我需要获取整个页面中的所有文本。

最佳答案

您可以通过指定 True 直接选择文本节点,而不是选择和过滤所有元素/标签。为 string argument :

for text_node in soup.find_all(string=True):
# do something with each text node

正如文档所述, string参数是 4.4.0 版本中的新参数,这意味着您可能需要使用 text参数而不是取决于您的版本:
for text_node in soup.find_all(text=True):
# do something with each text node

这是替换值的相关代码:
def remove_smart_quotes (text):
return text.replace(u"\u2018", "'") \
.replace(u"\u2019", "'") \
.replace(u"\u201c", '"') \
.replace(u"\u201d", '"')

soup = BeautifulSoup(html, 'lxml')

for text_node in soup.find_all(string=True):
text_node.replaceWith(remove_smart_quotes(text_node))

作为旁注,Beautiful Soup 文档实际上有一个 section on smart quotes .

关于python - 替换 Beautiful Soup 中的所有智能引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42444559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com