2016-05-24 31 views
-1

コード:はAttributeErrorは: 'NoneType' オブジェクトは、Pythonには属性 '下' を持っていない3.4

import requests 
from bs4 import BeautifulSoup 
import operator 
def start(url): 
    word_list =[] 
    source_code =requests.get(url).text 
    soup = BeautifulSoup(source_code) 
    for post_string in soup.find_all('a',{'class':'cb-skin-ads-link'}): 
     content = post_string.string 
     words = content.lower() 
     for each_word in words: 
      print(each_word) 
      word_list.append(each_word) 

start('http://www.cricbuzz.com/live-cricket-scorecard/16445/gl-vs-rcb-qualifier-1-indian-premier-league-2016') 

エラー:何らかの理由で

Traceback (most recent call last): 
    File "C:/Users/Shera/PycharmProjects/Begin/wordcount.py", line 15, in <module> 
    start('http://www.cricbuzz.com/live-cricket-scorecard/16445/gl-vs-rcb-qualifier-1-indian-premier-league-2016') 
    File "C:/Users/Shera/PycharmProjects/Begin/wordcount.py", line 10, in start 
    words = content.lower() 
AttributeError: 'NoneType' object has no attribute 'lower' 
+2

あなたの質問はありますか? –

+0

'content'は' string'ではありません... – henrikstroem

+1

そのアンカータグにはテキストがありませんので、実際に何を解析しようとしていますか? –

答えて

0

、はなしあるコンテンツ。デバッガを試したり、ステートメントを印刷してsoup.find_allから取得しているものを確認してください。リストは空ではないようですが、文字列少なくとも1つの要素の属性はNoneです。

+0

あなたの編集後、投稿にはあまりにも多くのコードがあり、 "beautifulsoup"タグを追加するには十分な説明がありません - 追加できますか? –

1

何が起こっているのかを理解するには、documentationをご覧ください。 .stringNoneだろうとき場合があります:

If a tag contains more than one thing, then it’s not clear what .string should refer to, so .string is defined to be None

あなたにも考慮要素の子を取る代わりget_text()を使用してになります。これはに役立つだろうと

content = post_string.get_text() 

注意エラーを回避しますが、見つかった要素には実際にテキストが含まれていないため、出力は表示されません。

<a target="_blank" href="Javascript:void(0)" class="cb-skin-ads-link cb-skin-ads-link-fixed ad-skin" rel="noreferrer"></a> 
関連する問題