1

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 ではありません (またはナビゲート可能な文字列でさえありますか?))。私はかなり混乱しています...

4

2 に答える 2

1

description = description(ext)ループの最初の繰り返しの後で、関数を文字列に置き換えます。についても同じですembed

于 2012-06-10T04:58:04.597 に答える
0
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

私はそれらの2つの機能がreturnとであるdescription()と思います。これらの2つのオブジェクトはではありません。embed()'NavigableString' object'unicode' objectcallable

したがって、次のように、これらの2行を置き換える必要があります。

for ext in exts:
    description_result = description(ext)
    embed_result = embed(ext)
于 2012-06-10T06:12:04.720 に答える