Python 3.x 美丽之群:可以';t将导航字符串转换为字符串
我开始学习Python,并决定编写一个简单的scraper。我遇到的一个问题是无法将NavigableString转换为常规字符串 使用BeautifulSoup4和Python 3.5.1。我是否应该咬紧牙关,使用Python和BeautifulSoup的早期版本?还是有办法 我可以编写自己的函数,将NavigableString转换为常规unicode字符串Python 3.x 美丽之群:可以';t将导航字符串转换为字符串,python-3.x,beautifulsoup,Python 3.x,Beautifulsoup,我开始学习Python,并决定编写一个简单的scraper。我遇到的一个问题是无法将NavigableString转换为常规字符串 使用BeautifulSoup4和Python 3.5.1。我是否应该咬紧牙关,使用Python和BeautifulSoup的早期版本?还是有办法 我可以编写自己的函数,将NavigableString转换为常规unicode字符串 for tag in soup.find_all("span"): for child in tag.children:
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'))
您可以这样做:
unicode(tag.string)
我想到了这个问题,并得到了来自with的马克·拉姆森的最佳答案
对于Python3,答案仅仅是
str(tag.string)
其他答案将失败。
unicode()
不是Python 3中内置的
tag.string.encode('utf-8')
会将字符串转换为您不想要的字节字符串。不知道您为什么被否决,这不仅对我有用,而且因为它是内置的。@Merritt,我也是!你能提供什么工作的完整例子吗?您提供的代码片段没有明确说明解决方案是什么。您的str(child.encode('utf-8')代码片段在上述循环中的位置?
import unidecode
word = unidecode.unidecode(tag.string)