基于Python的Twitter Bot问题

时间:2017-08-14 10:40:34

标签: python twitter bots

以下是我为Python中的基本Twitter机器人编写的代码

import tweepy
consumer_key='***'##### insert consumer Key
consumer_key='***'##### insert consumer Key
consumer_secret='***'### insert consumer secret
access_token='****'### insert access token
access_token_secret='***'####insert here
auth=tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api=tweepy.API(auth)

上面的代码工作正常。要运行上面的块并让它发布一条消息,我添加了以下代码。

api.update_status(status="This is a sample tweet using Tweepy with python")

当我运行此代码时,错误显示如下

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/tweepy/binder.py", line 187, in execute
proxies=self.api.proxy)
 File "/usr/lib/python3/dist-packages/requests/sessions.py", line 454, 
 in request
 prep = self.prepare_request(req)
 File "/usr/lib/python3/dist-packages/requests/sessions.py", line 388, 
 in prepare_request
 hooks=merge_hooks(request.hooks, self.hooks),
 File "/usr/lib/python3/dist-packages/requests/models.py", line 297, 
 in prepare
 self.prepare_auth(auth, url)
 File "/usr/lib/python3/dist-packages/requests/models.py", line 490, 
 in prepare_auth
 r = auth(self)
 File "/usr/lib/python3/dist-
 packages/requests_oauthlib/oauth1_auth.py", line 71, in __call__
 r.url = to_native_str(r.url)
 File "/usr/lib/python3/dist-
 packages/requests_oauthlib/oauth1_auth.py", line 14, in to_native_str
 return string.decode('utf-8')
 AttributeError: 'str' object has no attribute 'decode'

During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/usr/lib/python3/dist-
 packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, 
 in runfile
 execfile(filename, namespace)
 File "/usr/lib/python3/dist-
 packages/spyderlib/widgets/externalshell/sitecustomize.py", line 88, 
 in execfile
 exec(compile(open(filename, 'rb').read(), filename, 'exec'), 
 namespace)
 File "/home/compaq/Bot3.py", line 13, in <module>
 api.update_status(status="This is a sample tweet using Tweepy with 
 python")
 File "/usr/lib/python3/dist-packages/tweepy/api.py", line 194, in 
 update_status
 )(post_data=post_data, *args, **kwargs)
 File "/usr/lib/python3/dist-packages/tweepy/binder.py", line 245, in 
 _call
 return method.execute()
 File "/usr/lib/python3/dist-packages/tweepy/binder.py", line 189, in 
 execute
 raise TweepError('Failed to send request: %s' % e)
 tweepy.error.TweepError: Failed to send request: 'str' object has no 
 attribute 'decode'

我不确定错误在哪里。请求有人指导我。

0 个答案:

没有答案