类型错误:需要字符串或缓冲区,用户未找到
我需要爬取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库来进行爬取。我想做的是:
我想获取一个用户的朋友,以及这些朋友的朋友。当我在一个循环里面再放一个循环时,就出现了错误。以下是我的代码:
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
有什么建议吗?
提前谢谢你们。祝好!
1 个回答
6
看起来 get_friends
这个函数会返回一个用户对象的列表,所以你不需要对它里面的每个用户再调用 get_user
。你只需要直接使用:
for friend in friends:
for f in friend.get_friends(limit='200'):
...