python - AttributeError:在 Ubuntu 中运行 Python tweepy 脚本时出现解码错误

标签 python ubuntu error-handling tweepy attributeerror

我有一个 Python 3.5 脚本,它本质上是一个 Twitter 爬虫,它使用 tweepy 包及其 StreamListener 函数收集推文。

现在我的脚本在 Command shell 中运行完美,但是当我尝试在服务器的 Ubuntu 环境中运行它时,我收到解码错误。

   File "/usr/lib/python3/dist-packages/tweepy/streaming.py", line 445, in filter
self._start(async)
   File "/usr/lib/python3/dist-packages/tweepy/streaming.py", line 361, in _start
self._run()
   File "/usr/lib/python3/dist-packages/tweepy/streaming.py", line 294, in _run
raise exception
   File "/usr/lib/python3/dist-packages/tweepy/streaming.py", line 247, in _run
verify=self.verify)
   File "/usr/lib/python3/dist-packages/requests/sessions.py", line 454, in reque                                                                                                             st
prep = self.prepare_request(req)
   File "/usr/lib/python3/dist-packages/requests/sessions.py", line 388, in prepa                                                                                                             
re_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 7                                                                                                             1, in __call__
r.url = to_native_str(r.url)
   File "/usr/lib/python3/dist-packages/requests_oauthlib/oauth1_auth.py", line 1                                                                                                             4, in to_native_str
return string.decode('utf-8')

AttributeError: 'str' object has no attribute 'decode'

这是有道理的,因为您无法解码已经是字符串的字符串。但我的问题是如何解决这个问题,因为这是包内发生的错误?

最佳答案

看起来问题出在requests-oauthlib,你运行的是它的旧版本,这个问题似乎从 4.0.1 开始就已经修复了。

升级你的系统(Ubuntu 16.10的打包版本是7.0):

sudo apt update && apt upgrade

或者使用pip安装:

sudo pip3 install requests-oauthlib

关于python - AttributeError:在 Ubuntu 中运行 Python tweepy 脚本时出现解码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48542005/

相关文章:

python - 如何查询多个表

Python 对象作为类(class)讲师的可选参数

python - 诊断间歇性/"lagged"Websocket数据收集

opencv - caffe 安装卡在 "Linking CXX shared library ../../lib/libcaffe.so"

node.js - 处理 Nodejs 的任何错误并且不返回堆栈跟踪

python - 缺少设置文件的适当 Python 异常类

python - 如何从任意方向的二维数组中提取一维轮廓(具有集成宽度)

c++ - libstdc++ 中从哪个版本的 std::regex 可用

java - 添加 Java 类

c++ - 使用node-ffi时如何防止Node崩溃