Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Tweepy获得3500条以上的推文?_Python_Api_Twitter_Tweepy - Fatal编程技术网

Python 如何使用Tweepy获得3500条以上的推文?

Python 如何使用Tweepy获得3500条以上的推文?,python,api,twitter,tweepy,Python,Api,Twitter,Tweepy,请我需要一些帮助,我正在两段时间之间获得tweet,但是代码运行正常,直到达到一定数量,而不是挂起。我收到的推特通常在3000到3400条之间。我需要更多的推文,即使这需要时间。。。我听说过一种方法,你可以运行程序,直到它达到最大请求,然后会有一个终止函数,它将继续请求,直到你得到所有请求 谢谢:) 导入tweepy 导入日期时间 导入xlsxwriter 消费者密钥=“” 消费者_secret=“” access_token=“” access_token_secret=“” auth=twe

请我需要一些帮助,我正在两段时间之间获得tweet,但是代码运行正常,直到达到一定数量,而不是挂起。我收到的推特通常在3000到3400条之间。我需要更多的推文,即使这需要时间。。。我听说过一种方法,你可以运行程序,直到它达到最大请求,然后会有一个终止函数,它将继续请求,直到你得到所有请求

谢谢:)

导入tweepy
导入日期时间
导入xlsxwriter
消费者密钥=“”
消费者_secret=“”
access_token=“”
access_token_secret=“”
auth=tweepy.OAuthHandler(使用者密钥,使用者密钥)
授权设置\u访问\u令牌(访问\u令牌,访问\u令牌\u密钥)
api=tweepy.api(auth,wait\u on\u rate\u limit=True,wait\u on\u rate\u limit\u notify=True)
用户名='x'
startDate=datetime.datetime(2019、10、29)
endDate=datetime.datetime(2020,2,15)
tweets=[]
tmpTweets=api.user\u时间线(用户名,计数=200,包含\u rts=False,tweet\u mode=“扩展”)
对于tmpTweets中的tweet:
如果tweet.created_位于<结束日期,tweet.created_位于>开始日期:
tweets.append(tweet)
而(tmpTweets[-1]。在>开始日期创建):
打印(“Last Tweet@”,tmpTweets[-1]。创建于“-获取更多”)
tmpTweets=api.user\u timeline(用户名,max\u id=tmpTweets[-1].id,count=200,include\u rts=False,tweet\u mode=“extended”)
对于tmpTweets中的tweet:
如果tweet.created_位于<结束日期,tweet.created_位于>开始日期:
tweets.append(tweet)
工作簿=xlsxwriter.workbook(“x_数据“+”.xlsx”)
工作表=工作簿。添加工作表()
工作表。写入字符串(0,0,“Tweet ID:”)
工作表。写入字符串(0,1,“创建日期:”)
工作表。写入字符串(0,2,“Tweet:”)
工作表。写入字符串(0,3,“回复状态ID:”)
工作表。写入字符串(0,4,“收藏夹:”)
工作表。写入字符串(0,5,“转发:”)
工作表。写入字符串(0,6,“屏幕名称:”)
行=1
对于推文中的推文:
如果(非tweet.retweeted)和('RT@'不在tweet.text中):
工作表.写入字符串(行,0,tweet.id)
工作表。写入字符串(行,1,str(tweet.created_at))
工作表。写入(第2行,tweet.text)
工作表。写入字符串(第3行,str(tweet.in-reply-to-status-id))
工作表。写入字符串(第4行,tweet.favorite\u计数)
工作表。写入字符串(第5行,tweet.retweet\u计数)
工作表。写入字符串(第6行,str(tweet.user.screen\u name))
行+=1
其他:
持续
工作簿.关闭()
打印(“Excel文件准备就绪”)

我认为您不能,因为这是一个限制:“此方法最多只能返回3200条用户最近的tweet。用户对其他状态的本机转发包含在该总数中,而不管请求此资源时是否将
include\u rts
设置为
false
。”正确,这不是tweepy限制,twitterapi将不允许您从时间线上检索超过3200条Tweets。如果您需要更多,您将使用premium full archive search API进入商业领域。
import tweepy

import datetime

import xlsxwriter

consumer_key = "" 
consumer_secret = ""
access_token = ""
access_token_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)



username = 'x'

startDate = datetime.datetime(2019, 10, 29)

endDate =   datetime.datetime(2020, 2, 15)



tweets = []

tmpTweets = api.user_timeline(username, count=200, include_rts = False, tweet_mode="extended")

for tweet in tmpTweets:

    if tweet.created_at < endDate and tweet.created_at > startDate:

        tweets.append(tweet)



while (tmpTweets[-1].created_at > startDate):

    print("Last Tweet @", tmpTweets[-1].created_at, " - fetching some more")

    tmpTweets = api.user_timeline(username, max_id = tmpTweets[-1].id, count=200, include_rts = False, tweet_mode="extended")

    for tweet in tmpTweets:

        if tweet.created_at < endDate and tweet.created_at > startDate:

            tweets.append(tweet)




workbook = xlsxwriter.Workbook("x_Data" + ".xlsx")

worksheet = workbook.add_worksheet()

worksheet.write_string(0, 0, "Tweet ID:")
worksheet.write_string(0, 1, "Created date:")
worksheet.write_string(0, 2, "Tweet:")
worksheet.write_string(0, 3, "Reply to status ID:")
worksheet.write_string(0, 4, "Favorites:")
worksheet.write_string(0, 5, "Retweets:")
worksheet.write_string(0, 6, "Screen name:")

row = 1

for tweet in tweets:

    if (not tweet.retweeted) and ('RT @' not in tweet.text):
        worksheet.write_string(row, 0, tweet.id)

        worksheet.write_string(row, 1, str(tweet.created_at))

        worksheet.write(row, 2, tweet.text)

        worksheet.write_string(row, 3, str(tweet.in_reply_to_status_id))

        worksheet.write_string(row, 4, tweet.favorite_count)

        worksheet.write_string(row, 5, tweet.retweet_count)

        worksheet.write_string(row, 6, str(tweet.user.screen_name))



        row += 1

    else:
        continue

workbook.close()

print("Excel file ready")