TypeError:强制转换为Unicode:需要字符串或缓冲区,找到用户

tro*_*nic 5 python loops web-crawler typeerror last.fm

我必须为用户抓取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
Run Code Online (Sandbox Code Playgroud)

我使用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
Run Code Online (Sandbox Code Playgroud)

任何建议?

提前致谢.问候!

int*_*jay 6

它看起来get_friends会返回一个User对象列表,因此您无需调用get_user其条目.只需使用:

for friend in friends:
    for f in friend.get_friends(limit='200'):
        ...
Run Code Online (Sandbox Code Playgroud)