【问题标题】:Global Name Error while using Tweepy使用 Tweepy 时出现全局名称错误
【发布时间】:2015-08-29 22:28:48
【问题描述】:

我对 python 很陌生,正在尝试学习如何创建基本的 twitter 机器人。我正在尝试使用流 API 并获取句柄名称。我从文件中检查是否已经对获取的句柄进行了发布。如果是,那么我忽略我发布的其他内容。当一个新的句柄出现时,代码可以工作,但是当一个重复的句柄出现时,我收到以下错误:

C:\Users\User\Desktop>Stream-Reply_new.py
Traceback (most recent call last):
  File "C:\Users\User\Desktop\Stream-Reply_new.py", line 50, in <module>
    twitter_stream.filter(track=['#musiclovers'])
  File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 430, in filter
    self._start(async)
  File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 346, in _start
    self._run()
  File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 286, in _run
    raise exception
NameError: global name 'status' is not defined

import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
import json
import time

consumer_key = 
consumer_secret = 
access_token = 
access_secret = 

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

def check():
    datafile = file('C:\Users\User\Desktop\Growth Handles.txt', 'r')
    found = False
    for line in datafile:
        if status.user.screen_name in line:
            found = True
            break
    return found


class MyListener(StreamListener):


    def on_status(self, status):
        f=status.user.screen_name
        if check() :
            pass
        else:
            Append=open('Growth Handles.txt' , 'a' )
            Append.write(f)
            Append.close()
            Reply='@' + f + ' Check out Tomorrowland 2014 Setlist . http://.... '
            api = tweepy.API(auth)
            api.update_status(status=Reply)
            time.sleep(45)
        return True

    def on_error(self, status):
        print(status)
        return True



twitter_stream = Stream(auth, MyListener())
twitter_stream.filter(track=['#musiclovers'])

【问题讨论】:

    标签: python tweepy


    【解决方案1】:

    我不是 tweepy 方面的专家,但我可以看到“状态”被用作全局名称但未定义的唯一地方是在“检查()”函数中,它试图检查 if status.user.screen_name in line: ,但状态未定义为全局名称。

    也许你应该将状态作为参数发送给这个函数 -

    def check(status):
        datafile = file('C:\Users\User\Desktop\Growth Handles.txt', 'r')
        found = False
        for line in datafile:
            if status.user.screen_name in line:
                found = True
                break
        return found
    
    def on_status(self, status):
        f=status.user.screen_name
        if check(status) :
            pass
    

    【讨论】:

      【解决方案2】:

      您可以使用 try except 编写代码。

      try:
          f=status.user.screen_name
          Append=open('Growth Handles.txt' , 'a' )
          Append.write(f)
          Append.close()
          Reply='@' + f + ' Check out Tomorrowland 2014 Setlist . http://.... '
          api = tweepy.API(auth)
          api.update_status(status=Reply)
          time.sleep(45)
      return True
      except Exception: 
        pass
      

      这样你可以处理你在 except 中的错误,但由于“通过”,主程序将继续运行。

      【讨论】:

        猜你喜欢
        • 2022-06-14
        • 1970-01-01
        • 2015-01-05
        • 2016-12-11
        • 1970-01-01
        • 2019-10-07
        • 2016-05-25
        • 2023-03-13
        • 1970-01-01
        相关资源
        最近更新 更多