类型错误:强制转换为 Unicode:需要字符串或缓冲区,用户发现

发布于 2024-08-28 07:35:08 字数 1905 浏览 9 评论 0原文

我必须为用户抓取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 lib 进行爬行。我想要做什么:

我想要得到一个用户的朋友和用户朋友的朋友。当我在另一个 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

有什么建议吗?

提前致谢。问候!

i have to crawl last.fm for users (university exercise). I'm new to python and get following error:

 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

i use the pylast lib for crawling. what i want to do:

i want to get a users friends and the friends of the users friends. the error occurs, when i have a for loop in another for loop. here's the code:

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

any advice?

thanks in advance. regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗地喜欢 2024-09-04 07:35:08

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

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

It looks like get_friends will return a list of User objects, so you don't need to call get_user on its entries. Just use:

for friend in friends:
    for f in friend.get_friends(limit='200'):
        ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文