BeautifulSoup (python) の navigablestrings と unicode に問題があります。
基本的に、YouTube から 4 つの結果ページを解析し、一番上の結果の拡張子 (youtube.com/watch?= の後の URL の終わり) をリストに入れています。
次に、他の 2 つの関数でリストをループします。1 つの関数で、次のエラーがスローされますTypeError: 'NavigableString' object is not callable
。しかし、もう一人は言うTypeError: 'unicode' object is not callable
。どちらもまったく同じ文字列を使用しています。
ここで何が間違っていますか?私の解析コードはおそらく完璧ではないことを知っています。私は BeautifulSoup と正規表現の両方を使用しています。以前は、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 ではありません (またはナビゲート可能な文字列でさえありますか?))。私はかなり混乱しています...