我在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 回答
description = description(ext)
在循环中第一次迭代后用字符串替换该函数 .embed
也是如此 .description()
和embed()
是功能 . 例如然后
我认为这两个函数
description()
和embed()
都返回'NavigableString' object
和'unicode' object
. 那两个对象不是callable
.所以你应该重新抛出这两行,例如: