首页 文章

BeautifulSoup中导航字符串和unicode的问题

提问于
浏览
1

我在BeautifulSoup(python)中遇到了navigablestrings和unicode的问题 .

基本上,我正在从youtube解析四个结果页面,并将顶部结果的扩展名(youtube.com/watch?=之后的url的结尾)放入列表中 .

然后我将列表循环到另外两个函数中,在一个函数上,它抛出了这个错误: TypeError: 'NavigableString' object is not callable . 但是,另一个人说 TypeError: 'unicode' object is not callable . 两者都使用完全相同的字符串 .

我在这做错了什么?我知道我的解析代码可能不完美,我正在使用BeautifulSoup和regex . 在过去每当我收到NavigableString错误时,我只是抛出一个“.encode('ascii','ignore')或简单地str(),这似乎有效 . 任何帮助都将不胜感激!

for url in urls:
        response = urllib2.urlopen(url)
        html = response.read()
        soup = BeautifulSoup(html)
        link_data = soup.findAll("a", {"class":"yt-uix-tile-link result-item-translation-title"})[0]
        ext = re.findall('href="/(.*)">', str(link_data))[0]
        if isinstance(ext, str):
            exts.append('http://www.youtube.com/'+ext.replace(' ',''))

然后:

for ext in exts:
        description = description(ext)
        embed = embed(ext)

我只添加了isinstance()行来尝试查看问题所在 . 当'str'更改为'unicode'时,exts列表为空(表示它们是字符串,而不是unicode(甚至是navigablestrings?)) . 我很困惑......

2 回答

  • 0

    description = description(ext) 在循环中第一次迭代后用字符串替换该函数 . embed 也是如此 .

  • 1
    for ext in exts:
        description = description(ext)
        embed = embed(ext)
    

    description()embed() 是功能 . 例如

    def description():  #this is a function
        return u'result'
    

    然后

    description = description(ext) 
    #now description is a unicode object, and it is not callable.
    #It is can't call like this description(ext) again
    

    我认为这两个函数 description()embed() 都返回 'NavigableString' object'unicode' object . 那两个对象不是 callable .

    所以你应该重新抛出这两行,例如:

    for ext in exts:
        description_result = description(ext)
        embed_result = embed(ext)
    

相关问题