如何获取包含特定用户名或名称的所有tweet

2024-03-29 13:29:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用tweepy库编写一个代码来收集包含特定用户id的所有tweet

为了实现这样的目标(假设我可以访问twitter API),我会做如下工作:

import pandas as pd
import numpy as np
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import API
from tweepy import Cursor

auth = OAuthHandler(twitter_credentials['CONSUMER_KEY'], twitter_credentials['CONSUMER_SECRET'])
auth.set_access_token(twitter_credentials['ACCESS_TOKEN'], twitter_credentials['ACCESS_TOKEN_SECRET'])
api = API(auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)

# Search word/hashtag value 
HashValue = '_austrian'
# search start date value. the search will start from this date to the current date.
StartDate = "2019-11-11" # yyyy-mm-dd

for tweet in Cursor(api.search,q=HashValue,count=1,lang="en",since=StartDate, tweet_mode='extended').items():
    print (tweet.created_at, tweet.full_text)

然而,这种方法似乎没有回报我的期望。我刚收到一系列的推特,上面提到了奥地利语。你知道吗

我应该怎么做才能得到包含奥地利语的tweet?你知道吗


Tags: fromimportauthapisearchsecretdateconsumer
1条回答
网友
1楼 · 发布于 2024-03-29 13:29:50

我要做的是改用这个包:GetOldTweets3

我使用了以下代码。你知道吗

tweetCriteria = got.manager.TweetCriteria().setQuerySearch('@_austrian')\
                                       .setSince("2019-11-11")\
                                       .setMaxTweets(10)
tweet = got.manager.TweetManager.getTweets(tweetCriteria)

目前,它将查找所有包含给定日期的'\u austrian'的tweet,并限制在代码上搜索10条tweet。根据需要调整。你知道吗

要循环浏览结果,您需要循环它。你知道吗

for item in tweet:
  print(item.username, item.text)

样本输出

HofmannAviation In the 1980s I joined a #tyrolean Airways Dash 7 pilot training flight to Courchevel in the French Alps. In the Cockpit also Armin Kogler @_austrian @AHoensbroech @Flugthier @AlexInAir @_ABierwirth_ #dash7 @courchevel @BBD_Aircraft @GabyAttersee @AgueraMartin @GuillaumeFaurypic.twitter.com/NULpX4WSkA

您可以在github页面上阅读更多关于如何控制搜索的内容。使用这个包可以获得的不仅仅是用户名和内容。你知道吗

相关问题 更多 >