2016-04-22 32 views
-2

私は、美しいスープを使用して、Twitterのユーザーの画像とプロフィールへのリンクを抽出しようとしています。しかし、私は、 'NoneType'オブジェクトに属性 'get'エラーがないことを得ています。ここで'NoneType'オブジェクトに属性 'get'エラーがありません

def extract_tweets(html): 

    soup = BeautifulSoup(html) 
    write_to_file('soup.txt', soup) 
    tweets = soup.find_all('li', attrs={'data-item-type':'tweet'}) 
    for tweet in tweets: 
     tweet_text = tweet.find('p', class_='tweet-text') 
     author_link = tweet.find('a', class_='js-user-profile-link').get('href') 
     author_avatar = tweet.find('img', class_='avatar').get('src') 

    return tweets 

参照

<li id="stream-item-tweet-723026869960871937" class="js-stream-item stream-item stream-item expanding-stream-item " data-item-type="tweet" data-item-id="723026869960871937"> 
    <div class="tweet js-stream-tweet js-actionable-tweet js-profile-popup-actionable original-tweet js-original-tweet has-cards has-content " data-component-context="tweet" data-has-cards="true" data-disclosure-type="" data-mentions="BarunSobtiSays" data-you-block="false" data-follows-you="false" data-you-follow="false" data-user-id="2895647851" data-name="chaitali mallick" data-screen-name="chaitalimallic1" data-permalink-path="/chaitalimallic1/status/723026869960871937" data-item-id="723026869960871937" data-tweet-id="723026869960871937"> 
    <div class="context"> </div> 
    <div class="content"> 
    <div class="stream-item-header"> 
    <a class="account-group js-account-group js-action-profile js-user-profile-link js-nav" data-user-id="2895647851" href="/chaitalimallic1"> 
    <img class="avatar js-action-profile-avatar" alt="" src="https://pbs.twimg.com/profile_images/636712795808002048/CEs9XLwq_bigger.jpg"> 
    <strong class="fullname js-action-profile-name show-popup-with-id" data-aria-label-part="">chaitali mallick</strong> 
    <span>‏</span> 
    <span class="username js-action-profile-name" data-aria-label-part=""> 
    <s>@</s> 
    <b>chaitalimallic1</b> 
    </span> 
    </a> 
    <small class="time"> 
    </div> 
    <div class="js-tweet-text-container"> 
    <p class="TweetTextSize js-tweet-text tweet-text" lang="hi" data-aria-label-part="0"> 
    Dil deke dard e mohabbat kiya hai.. Maine pyar kiya..pyar kiya.. pyar kiya hai.. 
    <a class="twitter-atreply pretty-link js-nav" data-mentioned-user-id="2895447336" dir="ltr" href="/BarunSobtiSays"> 
    <s>@</s> 
    <b>BarunSobtiSays</b> 
    </a> 
    miss you :(
    <a class="twitter-timeline-link u-hidden" dir="ltr" data-pre-embedded="true" href="">pic.twitter.com/5GE0uPYalh</a> 
    </p> 
    </div> 
+0

あなたは '(取得呼び出ししようとしているオブジェクトの一つ)が' 'Noneです'。恐らくあなたの 'tweet.find(...)'呼び出しの1つは、あなたが探しているものが見つからなかったことを示すために 'None'を返すでしょう。 – khelwood

答えて

-1

あなたの機能は、あなたが投稿したHTMLで正常に動作するためにhtmlです。しかし、あなたが正しい結果を返すかどうかはわかりません。多分このような何か?

def extract_tweets(html): 
    soup = BeautifulSoup(html) 
    write_to_file('soup.txt', soup) 
    ret = list() 
    tweets = soup.find_all('li', attrs={'data-item-type':'tweet'}) 
    for tweet in tweets: 
     tweet_text = tweet.find('p', class_='tweet-text').text.strip() 
     author_link = tweet.find('a', class_='js-user-profile-link').get('href') 
     author_avatar = tweet.find('img', class_='avatar').get('src') 
     ret.append({"text": tweet_text, 
        "author_link": author_link, 
        "author_avatar": author_avatar}) 

    return ret 

しかし、あなたが探しているのタグが実際にある場合はdefinetely HTMLになっているはずです。上そう)

関連する問題