2017-02-08 3 views
1

저는 Twitter API 인 Tweepy &을 사용하는 비단뱀 프로그램을 작성하고 있으며, 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 키를 제거했습니다. 트위터 웹 사이트를 사용하여 해당 키를 취소하십시오. –

답변

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' ])