【问题标题】:how to get every single tweets with a certain hashtag using tweepy and twitter REST API如何使用 tweepy 和 twitter REST API 获取带有特定主题标签的每条推文
【发布时间】:2020-04-06 11:42:02
【问题描述】:

对于一个数据可视化项目,我需要收集所有带有特定主题标签的推文(这可能吗?)。为此,我使用下面的代码。它使用 Tweepy 和 REST API。但是,它最多只能下载大约 2500 条推文或更少。我想知道如何解决这个限制。是否有专业订阅或其他我应该购买的东西或者我应该如何修改代码。

#!/usr/bin/python
# -*- coding: utf-8 -*-
# this file is configured for rtl language and farsi characters

import sys
from key import *
import tweepy

#imported from the key.py file
API_KEY =KAPI_KEY
API_SECRET =KAPI_SECRET
OAUTH_TOKEN =KOAUTH_TOKEN
OAUTH_TOKEN_SECRET =KOAUTH_TOKEN_SECRET

auth = tweepy.AppAuthHandler(API_KEY, API_SECRET)

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

if not api:
    print("Can't Authenticate")
    sys.exit(-1)

def write_unicode(text, charset='utf-8'):
    return text.encode(charset)

searchQuery = "#کرونا"  # this is what we're searching for
maxTweets = 100000  # Some arbitrary large number
tweetsPerQry = 100  # this is the max the API permits
fName = 'Corona-rest8.txt'  # We'll store the tweets in a text file.

sinceId = None

max_id = -1
tweetCount: int = 0
print("Downloading max {0} tweets".format(maxTweets))
with open(fName, "wb") as f:
    while tweetCount < maxTweets:
        try:
            if max_id <= 0:
                if not sinceId:
                    new_tweets = api.search(q=searchQuery, count=tweetsPerQry)
                else:
                    new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
                                            since_id=sinceId)
            else:
                if not sinceId:
                    new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
                                            max_id=str(max_id - 1))
                else:
                    new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
                                            max_id=str(max_id - 1),
                                            since_id=sinceId)
            if not new_tweets:
                print("No more tweets found")
                break
            for tweet in new_tweets:

                #print(tweet._json["created_at"])
                if str(tweet._json["user"]["location"])!="":
                    print(tweet._json["user"]["location"])
                myDict = json.dumps(tweet._json["text"], ensure_ascii=False).encode('utf8')+ "\n".encode('ascii')
                f.write(myDict)

            tweetCount += len(new_tweets)
            print("Downloaded {0} tweets".format(tweetCount))
            max_id = new_tweets[-1].id
        except tweepy.TweepError as e:
            # Just exit if any error
            print("some error : " + str(e))
            break
print("Downloaded {0} tweets, Saved to {1}".format(tweetCount, fName))

【问题讨论】:

    标签: python rest twitter tweepy


    【解决方案1】:

    tweepy API Reference for api.search() 对此提供了一些色彩:

    请注意,Twitter 的搜索服务以及扩展的搜索 API 并不意味着是推文的详尽来源。并非所有推文都会被编入索引或通过搜索界面提供。

    要直接回答您的问题,不可能从 API 获取详尽的推文列表(因为有几个限制)。但是,一些基于抓取的 Python 库可用于解决这些 API 限制,例如 @taspinar 的 twitterscraper

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 2012-12-03
      • 2017-12-10
      • 2013-06-02
      • 2013-09-24
      • 2014-04-15
      • 1970-01-01
      相关资源
      最近更新 更多