2017-02-08 21 views
1

В настоящее время я пишу программу python, которая использует Tweedy & API 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' ])