第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Tweepy 回復(fù)提及機(jī)器人

Tweepy 回復(fù)提及機(jī)器人

暮色呼如 2022-07-12 15:34:46
當(dāng)我運行此腳本以使用 Tweepy 回復(fù) Twitter 提及時,我不斷收到錯誤 NameError: name 'create_api' is not defined 我不知道為什么。我在這里想念什么?任何幫助將不勝感激。謝謝import tweepyimport loggingimport timedef create_api():    consumer_key = 'xxxxx'    consumer_secret = 'xxxxx'    access_token = 'xxxx-xxxx'    access_token_secret = 'xxxx'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)try:    api.verify_credentials()    except Exception as e:        logger.error("Error creating API", exc_info=True)        raise e    logger.info("API created")    return apidef check_mentions(api, keywords, since_id):    logger.info("Retrieving mentions")    new_since_id = since_id    for tweet in tweepy.Cursor(api.mentions_timeline,        since_id=since_id).items():        new_since_id = max(tweet.id, new_since_id)        if tweet.in_reply_to_status_id is not None:            continue        if any(keyword in tweet.text.lower() for keyword in keywords):            logger.info(f"Answering to {tweet.user.name}")            if not tweet.user.following:                tweet.user.follow()            api.update_status(                status="Please reach us via DM",                in_reply_to_status_id=tweet.id,            )    return new_since_iddef main():    api = create_api()    since_id = 1    while True:        since_id = check_mentions(api, ["help", "support"], since_id)        logger.info("Waiting...")        time.sleep(60)if __name__ == "__main__":    main()
查看完整描述

2 回答

?
繁華開滿天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗 獲得超4個贊

當(dāng)你回復(fù)時,使用 tweet.id_str 而不是 tweet.id



查看完整回答
反對 回復(fù) 2022-07-12
?
胡說叔叔

TA貢獻(xiàn)1804條經(jīng)驗 獲得超8個贊

似乎您的大多數(shù)錯誤都來自不正確的縮進(jìn)。這是我修復(fù)縮進(jìn)問題的代碼版本。


我明白了,因為使用庫NameError: name 'logger' is not defined的正確方法是使用而不是. 這似乎是一個小錯字。logginglogging.xlogger


修復(fù)該問題后,我得到了tweepy.error.TweepError: [{'code': 89, 'message': 'Invalid or expired token.'}],這是意料之中的,因為我沒有有效的令牌。


import tweepy

import logging

import time


def create_api():

    consumer_key = 'xxxxx'

    consumer_secret = 'xxxxx'

    access_token = 'xxxx-xxxx'

    access_token_secret = 'xxxx'


    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)

    try:

        api.verify_credentials()

    except Exception as e:

        logging.error("Error creating API", exc_info=True)

        raise e

    logging.info("API created")

    return api


def check_mentions(api, keywords, since_id):

    logging.info("Retrieving mentions")

    new_since_id = since_id

    for tweet in tweepy.Cursor(api.mentions_timeline,

        since_id=since_id).items():

        new_since_id = max(tweet.id, new_since_id)

        if tweet.in_reply_to_status_id is not None:

            continue

        if any(keyword in tweet.text.lower() for keyword in keywords):

            logging.info(f"Answering to {tweet.user.name}")


            if not tweet.user.following:

                tweet.user.follow()


            api.update_status(

                status="Please reach us via DM",

                in_reply_to_status_id=tweet.id,

            )

    return new_since_id


def main():

    api = create_api()

    since_id = 1

    while True:

        since_id = check_mentions(api, ["help", "support"], since_id)

        logging.info("Waiting...")

        time.sleep(60)


if __name__ == "__main__":

    main()

輸出:(顯示日志記錄正在捕獲錯誤)


ERROR:root:Error creating API

Traceback (most recent call last):

  File ".\stack15.py", line 16, in create_api

    api.verify_credentials()

  File "C:\Users\xxxxxxx\source\repos\PyProjects\py_3.7\lib\site-packages\tweepy\api.py", line 605, in verify_credentials

    )(**kargs)

  File "C:\Users\xxxxxxx\source\repos\PyProjects\py_3.7\lib\site-packages\tweepy\binder.py", line 250, in _call

    return method.execute()

  File "C:\Users\xxxxxxx\source\repos\PyProjects\py_3.7\lib\site-packages\tweepy\binder.py", line 233, in execute

    raise TweepError(error_msg, resp, api_code=api_error_code)

tweepy.error.TweepError: [{'code': 89, 'message': 'Invalid or expired token.'}]

Traceback (most recent call last):

  File ".\stack15.py", line 52, in <module>

    main()

  File ".\stack15.py", line 44, in main

    api = create_api()

  File ".\stack15.py", line 19, in create_api

    raise e

  File ".\stack15.py", line 16, in create_api

    api.verify_credentials()

  File "C:\Users\xxxxxxx\source\repos\PyProjects\py_3.7\lib\site-packages\tweepy\api.py", line 605, in verify_credentials

    )(**kargs)

  File "C:\Users\xxxxxxx\source\repos\PyProjects\py_3.7\lib\site-packages\tweepy\binder.py", line 250, in _call

    return method.execute()

  File "C:\Users\xxxxxxx\source\repos\PyProjects\py_3.7\lib\site-packages\tweepy\binder.py", line 233, in execute

    raise TweepError(error_msg, resp, api_code=api_error_code)

tweepy.error.TweepError: [{'code': 89, 'message': 'Invalid or expired token.'}]


查看完整回答
反對 回復(fù) 2022-07-12
  • 2 回答
  • 0 關(guān)注
  • 225 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號