gpt4 book ai didi

python - 类型错误:强制转换为 Unicode:需要字符串或缓冲区,用户已找到

转载 作者:太空狗 更新时间:2023-10-30 01:04:00 25 4
gpt4 key购买 nike

我必须为用户抓取 last.fm(大学练习)。我是 python 新手,遇到以下错误:

 Traceback (most recent call last):
File "crawler.py", line 23, in <module>
for f in user_.get_friends(limit='200'):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pylast.py", line 2717, in get_friends
for node in _collect_nodes(limit, self, "user.getFriends", False):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pylast.py", line 3409, in _collect_nodes
doc = sender._request(method_name, cacheable, params)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pylast.py", line 969, in _request
return _Request(self.network, method_name, params).execute(cacheable)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pylast.py", line 721, in __init__
self.sign_it()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pylast.py", line 727, in sign_it
self.params['api_sig'] = self._get_signature()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pylast.py", line 740, in _get_signature
string += self.params[name]
TypeError: coercing to Unicode: need string or buffer, User found

我使用 pylast 库进行抓取。我想做什么:

我想得到一个用户 friend 和用户 friend 的 friend 。当我在另一个 for 循环中有一个 for 循环时,就会发生错误。这是代码:

network = pylast.get_lastfm_network(api_key = API_KEY, api_secret = API_SECRET, username = username, password_hash = password_hash)
user = network.get_user("vidarnelson")

friends = user.get_friends(limit='200')

i = 1

for friend in friends:
user_ = network.get_user(friend)
print '#%d %s' % (i, friend)
i = i + 1

for f in user_.get_friends(limit='200'):
print f

有什么建议吗?

提前致谢。问候!

最佳答案

看起来 get_friends 将返回一个 User 对象列表,因此您不需要在其条目上调用 get_user。只需使用:

for friend in friends:
for f in friend.get_friends(limit='200'):
...

关于python - 类型错误:强制转换为 Unicode:需要字符串或缓冲区,用户已找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2629524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com