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

我正在使用tweepy库编写代码,以收集包含特定用户ID的所有推文。对于此示例,假设我要查找与Austrian Airlines

相关的所有推文

要实现这样的目标(假设我可以使用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)

但是,这种方法似乎并没有返回我期望的结果。我刚得到一系列推文,其中提到了奥地利一词。

我该怎么做才能只获得包含_austrian的推文?

lk_8715 回答:如何获取包含特定用户名或名称的所有推文

我要做的是改用以下软件包:GetOldTweets3

我使用了以下代码。

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

当前,它的设置是查找包含给定日期的“ _austrian”且限制在代码上进行10次tweet搜索的所有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页面上阅读更多有关如何控制搜索的信息。使用此软件包,您可以获得的不仅是用户名和内容。

本文链接:https://www.f2er.com/3115603.html

大家都在问