使用Tweepy通过Python导入推文

2024-05-17 01:20:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望你会注意到两件事。第一个是tweepy中的TweepError,第二个是Twitter中的TwitterError 。这两个错误在我的应用程序中引入了错误。我安装了很多次,但仍然面临这两个错误。我在Windows7上使用Python2.6

>>> import tweepy
>>> dir(tweepy)
['API', 'BasicAuthHandler', 'Cache', 'Cursor', 'DirectMessage', 'FileCache', 'Friendship', 'MemoryCache', 'ModelFactory', 'OAuthHandler', 'SavedSearch', 'SearchResult', 'Status', 'Stream', 'StreamListener', '***TweepError***', 'User', '__author__', '__builtins__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__version__', 'api', 'auth', 'binder', 'cache', 'cursor', 'debug', 'error', 'models', 'oauth', 'parsers', 'streaming', 'utils']
>>> import twitter
>>> dir(twitter)
['ACCESS_TOKEN_URL', 'AUTHORIZATION_URL', 'Api', 'CHARACTER_LIMIT', 'DEFAULT_CACHE', 'DirectMessage', 'Hashtag', 'List', 'REQUEST_TOKEN_URL', 'SIGNIN_URL', 'Status', 'StringIO', 'Trend', ***'TwitterError'***, 'Url', 'User', '_FileCache', '_FileCacheError', '__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', 'base64', 'calendar', 'datetime', 'gzip', 'httplib', 'md5', 'oauth', 'os', 'parse_qs', 'parse_qsl', 'rfc822', 'simplejson', 'sys', 'tempfile', 'textwrap', 'time', 'urllib', 'urllib2', 'urlparse']

我得到这个错误

twitter = Twitter(format="xml")

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    twitter = Twitter(format="xml")
NameError: name 'Twitter' is not defined

Tags: nameimporturlstatus错误dirtwitterauthor
3条回答

如果您试图从the official Python Twitter Tools page执行操作,则应首先使用适当的导入指令from twitter import *。或者阅读更多关于 importing modules in python的内容

你只是被twitter、python_twitter和tweepy的库弄糊涂了。首先阅读你想使用的库的文档,然后关注该库,而不是其他文档。此错误是因为您安装了twitter并正在阅读python_twitter的文档

替换:

twitter = Twitter(format="xml")

my_twitter = twitter.Twitter(format="xml")

注意,我还更改了本地创建的实例的名称,以便您可以继续使用twitter作为模块的名称

相关问题 更多 >