使用 Tweepy 检索 Twitter 数据 [英] Retrieving Twitter data using Tweepy

查看:27
本文介绍了使用 Tweepy 检索 Twitter 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Python 代码使用 Tweepy 库检索特定主题标签的 Twitter 数据,但问题是我需要检索特定时期,例如,从 2013 年 6 月 30 日到 2013 年 12 月 30 日.我该怎么做?

I'm using that Python code using Tweepy Library to retrieve Twitter data for a specific hashtag, but the question is i need to retrieve a specific period, for example, from 30 june2013 till 30 December 2013. How can I do that?

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

#setting up the keys
consumer_key = '……………….'
consumer_secret = '……………..' 
access_token = '……………….'
access_secret = '……………..'

class TweetListener(StreamListener):
# A listener handles tweets are the received from the stream.
#This is a basic listener that just prints received tweets to standard output

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

  def on_error(self, status):
    print (status)



#printing all the tweets to the standard output
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)



stream = Stream(auth, TweetListener())

t = u"#سوريا"
stream.filter(track=[t])

推荐答案

我仍在调查为什么使用 tweepy.Cursor(api.search, geocode=.., q=query, until=date ) 也许是因为这个原因.但是我可以通过这些步骤在两个日期之间使用 Tweepy 检索 Twitter 数据.

I am still investigating why I couldn't get the same results using tweepy.Cursor(api.search, geocode=.., q=query, until=date ) Maybe it's for this reason. But I could retrieve Twitter data using Tweepy between two dates bty going through these steps.

首先,我在开始日期和结束日期之间创建了一个日期生成器.

First, I created a generator of dates, between start Date and end Date.

def date_range(start,end):
   current = start
   while (end - current).days >= 0:
      yield current
      current = current + datetime.timedelta(seconds=1)  #Based on your need, but you could do it per day/minute/hour

然后,我创建了一个 Listener 以便我可以通过访问 status.created_at 来获取特定日期创建的推文

Then, I created a Listener so I can get the tweets that are created in specific day by accessing to status.created_at

您的代码应如下所示:

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


#Use your keys
consumer_key = '...'
consumer_secret = '...' 
access_token = '...'
access_secret = '...'


auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

def date_range(start,end):
   current = start
   while (end - current).days >= 0:
      yield current
      current = current + datetime.timedelta(seconds=1)  

class TweetListener(StreamListener):
    def on_status(self, status):
        #api = tweepy.API(auth_handler=auth)
        #status.created_at += timedelta(hours=900)

        startDate = datetime.datetime(2013, 06, 30)
        stopDate = datetime.datetime(2013, 10, 30)
        for date in date_range(startDate,stopDate):
            status.created_at = date
            print "tweet " + str(status.created_at) +"\n"
            print status.text + "\n"  
            # You can dump your tweets into Json File, or load it to your database

stream = Stream(auth, TweetListener(), secure=True, )
t = u"#Syria" # You can use different hashtags 
stream.filter(track=[t])

输出:

我只是打印了要检查的日期(我不想用政治推文向 StackOverflow 发送垃圾邮件).

I just printed the dates to check (I don't wanna spam StackOverflow with the political tweets).

tweet 2013-06-30 00:00:01

-------------------

tweet 2013-06-30 00:00:02

-------------------

tweet 2013-06-30 00:00:03

-------------------

tweet 2013-06-30 00:00:04

-------------------

tweet 2013-06-30 00:00:05

-------------------

tweet 2013-06-30 00:00:06

-------------------

tweet 2013-06-30 00:00:07

-------------------

tweet 2013-06-30 00:00:08

-------------------

tweet 2013-06-30 00:00:09

-------------------

这篇关于使用 Tweepy 检索 Twitter 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆