如何获取包含特定用户名或名称的所有推文
How to get all tweets containing a certain username or name
我正在使用 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)
然而,这种方法似乎 return 我所期望的。我刚收到一系列推文,其中提到了奥地利这个词。
我应该怎么做才能只获得包含 _austrian 的推文?
我会做的是改用这个包:GetOldTweets3
我使用了以下代码。
tweetCriteria = got.manager.TweetCriteria().setQuerySearch('@_austrian')\
.setSince("2019-11-11")\
.setMaxTweets(10)
tweet = got.manager.TweetManager.getTweets(tweetCriteria)
目前,它设置为查找包含给定日期的“_austrian”的所有推文,并且仅限于代码上的 10 次推文搜索。根据您的需要进行调整。
要遍历结果,您需要对其进行循环。
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 页面上阅读有关如何控制搜索的更多信息。使用此包,您可以获得的不仅仅是用户名和内容。
我正在使用 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)
然而,这种方法似乎 return 我所期望的。我刚收到一系列推文,其中提到了奥地利这个词。
我应该怎么做才能只获得包含 _austrian 的推文?
我会做的是改用这个包:GetOldTweets3
我使用了以下代码。
tweetCriteria = got.manager.TweetCriteria().setQuerySearch('@_austrian')\
.setSince("2019-11-11")\
.setMaxTweets(10)
tweet = got.manager.TweetManager.getTweets(tweetCriteria)
目前,它设置为查找包含给定日期的“_austrian”的所有推文,并且仅限于代码上的 10 次推文搜索。根据您的需要进行调整。
要遍历结果,您需要对其进行循环。
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 页面上阅读有关如何控制搜索的更多信息。使用此包,您可以获得的不仅仅是用户名和内容。