“API”对象没有使用 Tweepy 的属性“搜索”

Mel*_*J13 1 python api twitter search tweepy

我正在尝试为我正在做的一个项目抓取 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) 
Run Code Online (Sandbox Code Playgroud)

但是,我不断收到错误“API”对象没有属性“search”,来自“for tweet in tweepy.Cursor(api.search, q="@BNonnecke", count=450,since='2020-02- 28').items(50000):" 我不太清楚为什么,也不知道如何解决这个问题。

非常感谢!

And*_*per 8

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

API.search_tweets(q, *, geocode, lang, locale, result_type, count, until, since_id, max_id, include_entities)
Run Code Online (Sandbox Code Playgroud)

另外,请阅读代码中的注释 :-) 搜索 API 有 7 天的历史记录限制,因此搜索推文2020-02-28将仅返回运行代码之日之前 7 天内发布的推文。