我正在尝试开始使用 Groupy 编写 Groupme机器人。
有一些与此相关的帖子,但它们似乎在技术上更具挑战性,解决方案似乎效率不高,而且没有一个解决了我遇到的问题。
这是我到目前为止的代码:
import groupy
token = "{My Token}"
from groupy.client import Client
client = Client.from_token(token)
groups = list(client.groups.list_all())
for group in groups:
print(group)
首先,我不相信这是相关的,但是在 Eclipse 中,它警告说 groupy 是一个未使用的导入,尽管在我看来并非如此。
更重要的是,我遇到的问题的奇怪部分是错误消息在没有对上述代码进行任何编辑的情况下发生了变化。
这是我收到的一条错误消息: 在每条错误消息中,我都编辑了文件位置的开头......因为我很偏执
Traceback (most recent call last):
File "\Projects\Eclipse\eclipse-workspace\Gort's Cousin\groupme_bot.py", line 15, in <module>
<Group(name='One of my group's names')>
<Group(name='Another one of my group's names')>
print(group.name)
File "\Python\Python37-32\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 27-28: character maps to <undefined>
所以,这一次系统实际上返回了我所在的几个组。但是由于某种 unicode 错误而调用了一个异常。
在不更改任何代码的情况下,我也会收到此错误:
Traceback (most recent call last):
File "\Python\Python37-32\lib\site-packages\groupy\session.py", line 49, in data
return self.json()['response']
File "\Python\Python37-32\lib\site-packages\requests\models.py", line 808, in json
return complexjson.loads(self.text, **kwargs)
File "\Python\Python37-32\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "\Python\Python37-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "\Python\Python37-32\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 60865 (char 60864)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "\Eclipse\eclipse-workspace\Gort's Cousin\groupme_bot.py", line 10, in <module>
groups = list(client.groups.list_all())
File "\Python\Python37-32\lib\site-packages\groupy\pagers.py", line 61, in autopage
self.items = self.fetch_next()
File "\Python\Python37-32\lib\site-packages\groupy\pagers.py", line 51, in fetch_next
return self.fetch()
File "\Python\Python37-32\lib\site-packages\groupy\pagers.py", line 42, in fetch
return self.endpoint(**self.params)
File "\Python\Python37-32\lib\site-packages\groupy\api\groups.py", line 21, in _raw_list
return [Group(self, **group) for group in response.data]
File "\Python\Python37-32\lib\site-packages\groupy\session.py", line 51, in data
raise exceptions.InvalidJsonError(self._resp) from e
groupy.exceptions.InvalidJsonError: The JSON was incomplete/invalid
我很可能是不正确的,但在我看来,groupy 聚合组的方式和/或我如何设置我的项目来处理这些调用存在一些问题。
对这个问题的任何帮助都会很棒。
如果有必要,我还可以从 groupy 库中添加一些代码。
TLDR:使用 Groupy 在 Python 中编写 groupme 机器人时出现“UnicodeEncodeError”错误