2017-02-08 5 views
1

私は現在、Tweepy & Twitter APIを利用するpythonプログラムを作成しており、twitterのつぶやきからURIリンクを抽出します。つぶやきからURIを集める

これは現在のところ私のコードです。つぶやきからURIを出力するように変更するには(含まれている場合)?


#Import the necessary methods from tweepy library 
    from tweepy.streaming import StreamListener 
    from tweepy import OAuthHandler 
    from tweepy import Stream 

    #Variables that contains the user credentials to access Twitter API 
    access_token = "-" 
    access_token_secret = "" 
    consumer_key = "" 
    consumer_secret = "" 


    #This is a basic listener that just prints received tweets to stdout. 
    class StdOutListener(StreamListener): 

    def on_data(self, data): 
    print data 
    return True 

    def on_error(self, status): 
    print status 


    if __name__ == '__main__': 

    #This handles Twitter authetification and the connection to Twitter Streaming API 
    l = StdOutListener() 
    auth = OAuthHandler(consumer_key, consumer_secret) 
    auth.set_access_token(access_token, access_token_secret) 
    stream = Stream(auth, l) 

    #This line filter Twitter Streams to capture data by the keyword: '#NFL' 
    twitterator = stream.filter(track=[ '#NFL' ]) 

    for tweet in twitterator: 
    print "(%s) @%s %s" % (tweet["created_at"], tweet["user"]["screen_name"], tweet["text"]) 
    for url in tweet["entities"]["urls"]: 
     print " - found URL: %s" % url["expanded_url"] 

+1

私はあなたのコード内でのOAuthのキーを削除しました。 Twitterのウェブサイトを使用してそれらのキーを取り消してください。 –

答えて

0

は、私が存在する場合にのみ、印刷のURLにコードを変更した:

#Import the necessary methods from tweepy library 
import json 

from tweepy.streaming import StreamListener 
from tweepy import OAuthHandler 
from tweepy import Stream 

#Variables that contains the user credentials to access Twitter API 
access_token = "-" 
access_token_secret = "" 
consumer_key = "" 
consumer_secret = "" 


#This is a basic listener that just prints received tweets to stdout. 
class StdOutListener(StreamListener): 
    def on_data(self, data): 
     tweet = json.loads(data) 
     for url in tweet["entities"]["urls"]: 
      print " - found URL: %s" % url["expanded_url"] 
     return True 

    def on_error(self, status): 
     print status 


if __name__ == '__main__': 
    #This handles Twitter authetification and the connection to Twitter Streaming API 
    l = StdOutListener() 
    auth = OAuthHandler(consumer_key, consumer_secret) 
    auth.set_access_token(access_token, access_token_secret) 
    stream = Stream(auth, l) 

    #This line filter Twitter Streams to capture data by the keyword: '#NFL' 
    stream.filter(track=[ '#NFL' ]) 
関連する問題