【问题标题】:'API' object has no attribute 'search' using Tweepy“API”对象没有使用 Tweepy 的“搜索”属性
【发布时间】:2021-11-25 04:19:27
【问题描述】:

我正在尝试为我正在做的项目抓取 Twitter 个人资料。我有以下代码

from tweepy import OAuthHandler
import pandas as pd

"""I like to have my python script print a message at the beginning. This helps me confirm whether everything is set up correctly. And it's nice to get an uplifting message ;)."""

print("You got this!")

access_token = ''
access_token_secret = ''
consumer_key = ''
consumer_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)

tweets = []

count = 1

"""Twitter will automatically sample the last 7 days of data. Depending on how many total tweets there are with the specific hashtag, keyword, handle, or key phrase that you are looking for, you can set the date back further by adding since= as one of the parameters. You can also manually add in the number of tweets you want to get back in the items() section."""

for tweet in tweepy.Cursor(api.search, q="@BNonnecke", count=450, since='2020-02-28').items(50000):
    
    print(count)
    count += 1

    try: 
        data = [tweet.created_at, tweet.id, tweet.text, tweet.user._json['screen_name'], tweet.user._json['name'], tweet.user._json['created_at'], tweet.entities['urls']]
        data = tuple(data)
        tweets.append(data)

    except tweepy.TweepError as e:
        print(e.reason)
        continue

    except StopIteration:
        break

df = pd.DataFrame(tweets, columns = ['created_at','tweet_id', 'tweet_text', 'screen_name', 'name', 'account_creation_date', 'urls'])

"""Add the path to the folder you want to save the CSV file in as well as what you want the CSV file to be named inside the single quotations"""
df.to_csv(path_or_buf = '/Users/Name/Desktop/FolderName/FileName.csv', index=False) 

但是,我不断从“for tweet in tweepy.Cursor(api.search, q="@BNonnecke", count=450, since='2020 -02-28').items(50000):" 我不太确定为什么,也不知道如何解决这个问题。

非常感谢!

【问题讨论】:

    标签: python api search twitter tweepy


    【解决方案1】:

    最新版本的 Tweepy(v4 以上)现在有一个 search_tweets 方法而不是 search 方法。检查documentation

    API.search_tweets(q, *, geocode, lang, locale, result_type, count, until, since_id, max_id, include_entities)
    

    另外,请阅读您代码中的评论 :-) 搜索 API 有 7 天的历史记录限制,因此搜索自 2020-02-28 以来的推文只会返回在您运行代码之日前 7 天内发布的推文。

    【讨论】:

      猜你喜欢
      • 2021-11-25
      • 1970-01-01
      • 2017-04-19
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      相关资源
      最近更新 更多