twitter 抓取工具不适用于 tweepy v4.0.0.0^

问题描述 投票:0回答:0

这是我编写的代码,用于在推特上抓取推文中的特定关键字,然后将这些推文发送到专用的松弛频道:

import os
import tweepy
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from dotenv import load_dotenv

load_dotenv()  # Load environment variables from .env file

# Twitter API credentials
TWITTER_API_KEY = os.getenv('TWITTER_API_KEY')
TWITTER_API_SECRET_KEY = os.getenv('TWITTER_API_SECRET_KEY')
TWITTER_ACCESS_TOKEN = os.getenv('TWITTER_ACCESS_TOKEN')
TWITTER_ACCESS_TOKEN_SECRET = os.getenv('TWITTER_ACCESS_TOKEN_SECRET')

# Slack API credentials
SLACK_API_TOKEN = os.getenv('SLACK_API_TOKEN')

# Slack channel to send notifications to
SLACK_CHANNEL_ID = os.getenv('SLACK_CHANNEL_ID')

# Keywords to monitor
KEYWORDS = ['nft accounting', 'crypto accounting', 'crypto tax software']

class MyStream(tweepy.Stream):
    def __init__(self, auth, listener, max_tweets):
        super().__init__(auth=auth, listener=listener)
        self.max_tweets = max_tweets
        self.tweet_count = 0

    def on_status(self, status):
        if self.tweet_count >= self.max_tweets:
            self.disconnect()
        else:
            for keyword in KEYWORDS:
                if keyword.lower() in status.text.lower() and not status.retweeted and 'RT @' not in status.text and not status.user.verified and not status.user.default_profile_image and not status.user.bot:
                    try:
                        slack_client = WebClient(token=SLACK_API_TOKEN)
                        response = slack_client.chat_postMessage(
                            channel=SLACK_CHANNEL_ID,
                            text=f"Post with keyword \"{keyword}\" found:\n{status.entities['urls'][0]['expanded_url']}\nAuthor: {status.author.name}\nTimestamp: {status.created_at}\nExcerpt: {status.text}"
                        )
                    except SlackApiError as e:
                        print(f"Error sending message: {e}")
                    break
            self.tweet_count += 1

if __name__ == "__main__":
    # Authenticate with Twitter API
    auth = tweepy.OAuthHandler(TWITTER_API_KEY, TWITTER_API_SECRET_KEY)
    auth.set_access_token(TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET)

    # Authenticate with Slack API
    slack_client = WebClient(token=SLACK_API_TOKEN)

    myStreamListener = tweepy.StreamListener()
    myStream = MyStream(auth=auth, listener=myStreamListener, max_tweets=50)

    # Start streaming tweets containing the keywords
    myStream.filter(track=KEYWORDS, languages=['en'], exclude_replies=True, is_async=True)

但是,每当我运行脚本时,我都会从 tweepy 得到这个错误:

Traceback (most recent call last):
  File "C:\Users\Foster\twitterscrape\twitter_scraper.py", line 24, in <module>
    class MyStream(tweepy.Stream):
                   ^^^^^^^^^^^^^
AttributeError: module 'tweepy' has no attribute 'Stream'

我不确定我应该在 v4.0.0.0 及更高版本中包括什么,我已经查看了 tweepy 文档,我只是没有足够的 python 经验来知道在这里做什么。任何帮助表示赞赏。

python twitter tweepy
© www.soinside.com 2019 - 2024. All rights reserved.