BeautifulSoup:无法将NavigableString转换为字符串 [英] BeautifulSoup: Can't convert NavigableString to string

查看:944
本文介绍了BeautifulSoup:无法将NavigableString转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm starting to learn Python and I've decided to code a simple scraper. One problem I'm encountering is I cannot convert a NavigableString to a regular string.

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

Using BeautifulSoup4 and Python 3.5.1. Should I just bite the bullet and go to an earlier version of Python and BeautifulSoup? Or is there a way I can code my own function to cast a NavigableString to a regular unicode string?

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!

推荐答案

我尝试在应该编码的时候进行解码:

I tried to decode when I should have encoded:

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

这篇关于BeautifulSoup:无法将NavigableString转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆