簡體   English   中英

我的 tweepy Twitter 機器人不工作,我做錯了什么?

[英]My tweepy Twitter bot is not working, what am I doing wrong?

它返回幾個錯誤,其中大部分顯示: <framework not available>

我是編碼新手,我只是想用 Twitter API 制作一個有趣的機器人,但有很多錯誤,我不知道該怎么做。

出現的主要錯誤是這個:

(來自 collections import namedtuple,Mapping ImportError: cannot import name 'Mapping from 'collections')

有誰可以幫助我嗎?

import tweepy

# Authenticate to Twitter
auth = tweepy.OAuthHandler("", "")
auth.set_access_token("", "")
api = tweepy.API(auth)

# Pegar ID do bot
bot_id = int(api.me().id_str)


class MyStreamListener(tweepy.StreamListener):
    def on_status(self, tweet):
        print("tweet encontrado!")
        print(f"TWEET: {tweet.author.scream_name} - {tweet.text}")
        if tweet.author.id != bot_id and tweet.in_reply_to_status_id is None:
            if not tweet.retweeted:
                try:
                    print("Tentando retuite...")
                    api.retweet(tweet.id)
                    print("Retuitado com sucesso!")
                except Exception as err:
                    print(err)


stream_listener = MyStreamListener()
stream = tweepy.Stream(auth=api.auth, Listener=stream_listener)
stream.filter(track=['Squirrel', 'Squirrels', 'squirrel', 'squirrels', 'Esquilo', 'Esquilos'], languages=['en', 'pt'])

出現的錯誤:Traceback(最近調用最后一次):文件“C:\Users\wgama\PycharmProjects\botesquilo\botesquilo.py”,第 1 行,在導入 tweepy 文件“C:\Users\wgama\AppData\Local\ Programs\Python\Python310\lib\site-packages\ tweepy_init_.py ",第 12 行,在 from tweepy.api import API File "C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\ site-packages\tweepy\api.py",第 15 行,在導入請求文件“C:\Users\wgama\AppData\Local\Programs\Python\ Python310 \lib\site-packages\requests_init_.py”中,行43、在導入urllib3文件“C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\site-packages\ urllib3_init_.py ”,第8行,在from.connectionpool導入(文件“C: \Users\wgama\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py”,第 29 行,在 from.connection 導入(文件“C:\Users\wgama\AppData\Local\ Programs\Python\Python310\lib\site-packages\urllib3\connection.py", 第 39 行,在 from.util.ssl_ import ( File "C:\Users\wga ma\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\ util_init_.py ", 第 3 行,在 from.connection import is_connection_dropped File "C:\Users\wgama\AppData\Local\Programs \Python\Python310\lib\site-packages\urllib3\util\connection.py",第 3 行,在 from.wait import wait_for_read File "C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\ site-packages\urllib3\util\wait.py",第 1 行,在 from.selectors 導入(文件“C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util \selectors.py”,第 14 行,在 from collections import namedtuple 中,Mapping ImportError: cannot import name 'Mapping from 'collections' (C:\Users\wgama\AppData\Local\Programs\Python\ Python310 \lib\collections_init _.py)

正如評論中所調查的那樣,問題來自於您使用的是與 Python 3.10 不兼容的舊版本的 Tweepy。

因此,更新到最新版本的 Tweepy(當前為 4.8.0)應該可以解決您的問題。

但請注意, API.me()方法已從 Tweepy 4.0.0 版本中刪除,因此您必須將其替換為API.verify_credentials()方法。

編輯:我只是更仔細地閱讀了你的代碼,我可能發現了其他錯誤:推文 object 中沒有author字段,而是user字段,我猜你的意思是screen_name而不是scream_name (.),最后,在你更新到最新版本的 Tweepy,您還必須將tweepy.StreamListener替換為tweepy.Stream

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM