首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Tweets收集URI

从Tweets收集URI
EN

Stack Overflow用户
提问于 2017-02-08 22:36:13
回答 1查看 792关注 0票数 2

我目前正在编写一个python程序,它利用Tweepy & twitter,并从Twitter上的tweet中提取URI链接。

这是我目前的代码。如何修改它,使其只输出来自tweet的URI(如果其中包括一个)?

代码语言:javascript
复制
    #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"]
EN

回答 1

Stack Overflow用户

发布于 2017-02-09 18:37:06

我已经修改了您的代码,以便只打印URL,如果有:

代码语言:javascript
复制
#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' ])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42124950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档