BeautifulSoup:无法将NavigableString转换为字符串

Sau*_*tin 5 beautifulsoup python-3.x

我开始学习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!
Run Code Online (Sandbox Code Playgroud)

Sau*_*tin 7

我试图解码我应该编码:

str(child.encode('utf-8'))
Run Code Online (Sandbox Code Playgroud)


Kon*_*hog 7

对于 Python 3,答案仅仅是 str(tag.string)

其他答案将失败。

unicode() 不是 Python 3 内置的。

tag.string.encode('utf-8') 会将字符串转换为您不想要的字节字符串。