标签: tweepy

Python 2.7中的urllib.request

我可以在Python 3.1中使用urllib.request模块.但是当我使用Python 2.7执行相同的程序时,出现了一个错误:

AttributeError: 'module' object has no attribute 'request'.

我相信这个错误是因为在urllib中没有Python 2.7的请求模块.因为我需要使用tweepy,所以我必须坚持使用Python 2.7,因为tweepy不支持Python 3.

那么我如何在Python 2.7中使用urllib.request模块呢?

python twitter tweepy

20
推荐指数
2
解决办法
7万
查看次数

如何扩展python模块?在`python-twitter`包中添加新功能

扩展现有Python模块的最佳实践是什么 - 在这种情况下,我想python-twitter通过向基本API类添加新方法来扩展包.

我看过了tweepy,我也喜欢这样; 我发现我python-twitter更容易理解并扩展我想要的功能.

我已经编写了方法 - 我正在试图找出将Pythonic和破坏性最小的方法添加到python-twitter包模块中,而不是改变这个模块的核心.

python module python-module python-twitter tweepy

18
推荐指数
3
解决办法
2万
查看次数

用Tweepy回复Tweet - Python

回复一个特定的推文时,我似乎无法解决这个问题:

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)

api = tweepy.API(auth)

### at this point I've grabbed the tweet and loaded it to JSON...

tweetId = tweet['results'][0]['id']

api.update_status('My status update',tweetId)
Run Code Online (Sandbox Code Playgroud)

api说它需要可选参数而in_reply_to_status_id是第一个,但它似乎完全忽略了它.此脚本将发布更新状态,但它不会将其链接为对我传递的tweetId的回复.

API供参考:http://code.google.com/p/tweepy/wiki/APIReference#update_status

有人有主意吗?我觉得我在这里错过了一些简单的东西......

提前致谢.

python twitter tweepy

18
推荐指数
4
解决办法
1万
查看次数

用tweepy返回用户推文

我使用tweepy和python 2.7.6来返回指定用户的推文

我的代码看起来像:

import tweepy

ckey = 'myckey'
csecret = 'mycsecret'
atoken = 'myatoken'
asecret = 'myasecret'



auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

api = tweepy.API(auth)

stuff = api.user_timeline(screen_name = 'danieltosh', count = 100, include_rts = True)

print stuff
Run Code Online (Sandbox Code Playgroud)

然而,这会产生一组看起来像的消息<tweepy.models.Status object at 0x7ff2ca3c1050>

是否可以从这些对象中打印出有用的信息?我在哪里可以找到他们的所有属性?

python twitter tweepy

18
推荐指数
2
解决办法
2万
查看次数

使用Tweepy收听流并搜索推文.如何停止以前的搜索并只收听新流?

我正在使用Flask和Tweepy来搜索实时推文.在前端,我有一个用户文本输入,以及一个名为"搜索"的按钮.理想情况下,当用户在输入中提供搜索词并单击"搜索"按钮时,Tweepy应该侦听新的搜索词并停止前一个搜索词流.单击"搜索"按钮时,它将执行此功能:

@app.route('/search', methods=['POST'])
# gets search-keyword and starts stream
def streamTweets():
    search_term = request.form['tweet']
    search_term_hashtag = '#' + search_term
    # instantiate listener
    listener = StdOutListener()
    # stream object uses listener we instantiated above to listen for data
    stream = tweepy.Stream(auth, listener)

    if stream is not None:
        print "Stream disconnected..."
        stream.disconnect()

    stream.filter(track=[search_term or search_term_hashtag], async=True)
    redirect('/stream') # execute '/stream' sse
    return render_template('index.html')
Run Code Online (Sandbox Code Playgroud)

/stream上面代码中第二行到最后一行执行的路由如下:

@app.route('/stream')
def stream():
    # we will use Pub/Sub process to send real-time tweets to client
    def event_stream(): …
Run Code Online (Sandbox Code Playgroud)

python tweepy flask

18
推荐指数
1
解决办法
2531
查看次数

通过tweepy从"user_timeline"获取完整的推文文本

我使用tweepy使用此处包含的脚本从用户的时间线获取推文.但是,这些推文正在被截断:

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
new_tweets = api.user_timeline(screen_name = screen_name,count=200, full_text=True)
Run Code Online (Sandbox Code Playgroud)

返回:

Status(contributors=None, 
     truncated=True, 
     text=u"#Hungary's new bill allows the detention of asylum seekers 
          &amp; push backs to #Serbia. We've seen push backs before so\u2026 https:// 
          t.co/iDswEs3qYR", 
          is_quote_status=False, 
          ...
Run Code Online (Sandbox Code Playgroud)

也就是说,对某些人来说i,new_tweets[i].text.encode("utf-8")似乎是

#Hungary's new bill allows the detention of asylum seekers &amp; 
push backs to #Serbia. We've seen push backs before so…https://t.co/
iDswEs3qYR
Run Code Online (Sandbox Code Playgroud)

...在这通常会在Twitter上显示后更换文本.

有谁知道如何覆盖truncated=True我的请求获取全文?

python twitter tweepy

17
推荐指数
2
解决办法
1万
查看次数

带有readlines()方法的Python3 UnicodeDecodeError

试图创建一个读取行并发布它们的twitter机器人.使用Python3和tweepy,通过我的共享服务器空间上的virtualenv.这是代码中似乎有问题的一部分:

#!/foo/env/bin/python3

import re
import tweepy, time, sys

argfile = str(sys.argv[1])

filename=open(argfile, 'r')
f=filename.readlines()
filename.close()
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

该错误特别指向错误f=filename.readlines()的来源.知道什么可能是错的吗?谢谢.

python unicode sys tweepy python-3.x

16
推荐指数
3
解决办法
2万
查看次数

403 禁止 453 - 您当前只能访问 Twitter API v2 端点的子集和有限的 v1.1 端点

我最近注册了免费级别的 Twitter API,我想使用 Tweepy 来帮助我从用户那里提取推文。

api_key = config['twitter']['api_key']
api_secret = config['twitter']['api_key_secret']

access_token = config['twitter']['access_token']
access_token_secret = config['twitter']['access_token_secret']

auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)
tweets = api.home_timeline()
Run Code Online (Sandbox Code Playgroud)

但我运行后,出现以下错误:

禁止:403 禁止 453 - 您当前只能访问 Twitter API v2 端点的子集和有限的 v1.1 端点(例如媒体发布、oauth)。如果您需要访问此端点,您可能需要不同的访问级别。您可以在这里了解更多信息: https: //developer.twitter.com/en/portal/product

如果我理解正确的话,Twitter API 文档确实允许免费级别的访问。但是,我不确定为什么它对我不起作用。

有专业人士知道如何解决这个问题吗?

python twitter nlp tweepy

16
推荐指数
1
解决办法
2万
查看次数

如何正确关闭tweepy流

我正在试图找出如何正确关闭异步tweepy流.

可以在这里找到tweepy流模块.

我像这样开始流:

stream = Stream(auth, listener)
stream.filter(track=['keyword'], async=True)
Run Code Online (Sandbox Code Playgroud)

关闭应用程序时,我尝试关闭流,如下所示:

stream.disconnect()
Run Code Online (Sandbox Code Playgroud)

这个方法似乎按预期工作但似乎有一个问题:流线程仍然在循环的中间(等待/处理推文)并且直到下一个循环才被杀死,所以当流接收到推文时甚至之后应用程序已关闭,它仍然尝试调用侦听器对象(这可以通过侦听器对象上的简单打印语法看到).我不确定这是不是坏事,或者它是否可以简单地被忽略.

我有两个问题:

  1. 这是关闭流的最佳方式还是我应采取不同的方法?
  2. 不应该将异步线程创建为守护程序线程吗?

python multithreading tweepy

15
推荐指数
1
解决办法
7305
查看次数

从tweepy异常实例获取错误代码

我是python的新手,我正在尝试使用库.它提出了一个例外,我试图找出哪一个.这就是我想要的:

except tweepy.TweepError as e:
    print e
    print type(e)
    print e.__dict__
    print e.reason
    print type(e.reason)
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

[{u'message': u'Sorry, that page does not exist', u'code': 34}]
<class 'tweepy.error.TweepError'>
{'reason': u"[{u'message': u'Sorry, that page does not exist', u'code': 34}]", 'response': <httplib.HTTPResponse instance at 0x00000000029CEAC8>}
[{u'message': u'Sorry, that page does not exist', u'code': 34}]
<type 'unicode'>
Run Code Online (Sandbox Code Playgroud)

我试图达到那个代码.我试过e.reason.code没有成功,我不知道该尝试什么.

python twitter tweepy python-2.7

15
推荐指数
3
解决办法
1万
查看次数