首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >找不出Tweepy Twitter搜索API代码

找不出Tweepy Twitter搜索API代码
EN

Stack Overflow用户
提问于 2014-07-14 00:15:43
回答 1查看 2.7K关注 0票数 0

我对Python非常陌生,几周前我还自学过它。我尝试用Tweepy拼凑一些简单的脚本,用Twitter做各种事情。我一直试图使搜索API工作,但没有任何效果。下面的代码只是简单地搜索Twitter的最后7天的关键字。

代码语言:javascript
运行
复制
# 1.Import required libs and used objects/libs 
import tweepy
from tweepy import OAuthHandler
from tweepy import API
from tweepy import Cursor



#2. GET or input App keys and tokens. Here keys/tokens are pasted from Twitter.
ckey = 'key'
csecret = 'secret'
atoken = 'token'
asecret =  'secret'

# 3. Set authorization tokens. 
auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

#4. Define API. 
api = tweepy.API(auth)

#5. Define list or library.

for tweets in tweepy.Cursor(api.search, q = '#IS', count = 100,
                           result_type ='recent').items():
print tweet.text

每次我收到以下错误时:

代码语言:javascript
运行
复制
Traceback (most recent call last):
  File "C:/Users/jbutk_001/Desktop/Tweety Test/tweepy streaming.py", line 25, in <module>
     result_type ='recent')).items():
   File "build\bdist.win-amd64\egg\tweepy\cursor.py", line 22, in __init__
    raise TweepError('This method does not perform pagination')
TweepError: This method does not perform pagination

我也试过

代码语言:javascript
运行
复制
for tweets in tweepy.Cursor(api.search(q = '#IS', count = 100,
                           result_type ='recent')).items():
print tweet.text

但我得到了:

代码语言:javascript
运行
复制
Traceback (most recent call last):
  File "C:/Users/jbutk_001/Desktop/Tweety Test/tweepy streaming.py", line 25, in   <module>
    result_type ='recent').items():
  File "build\bdist.win-amd64\egg\tweepy\cursor.py", line 181, in next
     self.current_page = self.page_iterator.next()
   File "build\bdist.win-amd64\egg\tweepy\cursor.py", line 101, in next
    old_parser = self.method.__self__.parser
 AttributeError: 'function' object has no attribute '__self__'

谁能帮我指出正确的方向,这几天来一直让我抓狂。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-06 21:54:13

首先,您导入了一些错误的东西,您可能想了解更多关于导入如何工作的内容:What are good rules of thumb for Python imports?

关于如何使这种搜索工作的一个工作示例:

代码语言:javascript
运行
复制
import tweepy

CONSUMER_KEY = 'key'
CONSUMER_SECRET = 'secret'
ACCESS_KEY = 'accesskey'
ACCESS_SECRET = 'accesssecret'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

for tweet in tweepy.Cursor(api.search,
                       q="#IS",
                       count=100,
                       result_type="recent",
                       include_entities=True,
                       lang="en").items():
print tweet.tweet

此外,我建议避免使用文件名中的空格,而不是"tweepy streaming.py“,使用类似"tweepy_streaming.py”的内容。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24728082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档