Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am having some problems with navigablestrings and unicode in BeautifulSoup (python).
Basically, I am parsing four results pages from youtube, and putting the top result's extension (end of the url after youtube.com/watch?=) into a list.
I then loop the list in two other functions, on one, it throws this error:
TypeError: 'NavigableString' object is not callable
. However, the other one says
TypeError: 'unicode' object is not callable
. Both are using the same exact string.
What am I doing wrong here? I know that my parsing code is probably not perfect, I'm using both BeautifulSoup and regex. In the past whenever I get NavigableString errors, I have just thrown in a ".encode('ascii', 'ignore') or simply str(), and that has seemed to work. Any help would be appreciated!
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(' ',''))
and then:
for ext in exts:
description = description(ext)
embed = embed(ext)
i only added the isinstance() lines to try and see what the problem was. When 'str' is changed to 'unicode', the exts list is empty (meaning they are strings, not unicode (or even navigablestrings?)). I'm quite confused...
–
–
–
–
description()
and embed()
are function. For example
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
I think those two function description()
and embed()
are return 'NavigableString' object
and 'unicode' object
. Those two object are not callable
.
So you should repalce those two line , such as:
for ext in exts:
description_result = description(ext)
embed_result = embed(ext)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.