gpt4 book ai didi

python-3.x - BeautifulSoup:无法将NavigableString转换为字符串

转载 作者:行者123 更新时间:2023-12-04 17:08:17 26 4
gpt4 key购买 nike

我开始学习Python,并决定编写一个简单的刮板代码。我遇到的一个问题是我无法将NavigableString转换为常规字符串。

使用BeautifulSoup4和Python 3.5.1。我是否应该咬一口子,然后转到Python和BeautifulSoup的早期版本?还是有办法
我可以编写自己的函数以将NavigableString转换为常规unicode字符串吗?

for tag in soup.find_all("span"):
for child in tag.children:
if "name" in tag.string: #triggers error, can't compare string to NavigableString/bytes
return child

#things i've tried:
#if "name" in str(tag.string)
#if "name" in unicode(tag.string) #not in 3.5?
#if "name" in strring(tag.string, "utf-8")
#tried regex, didn't work. Again, doesn't like NavigableSTring type.
#... bunch of other stuff too!

最佳答案

当我应该编码时,我尝试解码:

str(child.encode('utf-8'))

关于python-3.x - BeautifulSoup:无法将NavigableString转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35329098/

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