从klout异常KloutHTTPError中检索数据

时间:2014-05-07 11:18:26

标签: python klout

我正在尝试使用kloud python API检索klouID。我从mongo数据库获取用户名,我正在尝试使用此用户名检索kloudid。我注意到我只能为某些用户检索ID(他们或他们的一些关注者在klout中注册)。因此,我想创建一个尝试 - 除了为了克服klout无法返回kloudid的用户的raise KloutHTTPError( e, uri) klout.api.KloutHTTPError: ERROR: HTTP Error 404: Not Found错误。

我的代码:

for cursor in collection.find().limit(100):
    name =  cursor['database'].get('name')
    print name
    kloutId = k.identity.klout(screenName=name).get('id')
    score = k.user.score(kloutId=kloutId).get('score')
    print "User's klout score is: %s" % (score)
    # By default all communication is not secure (HTTP). An optional secure parameter
    # can be sepcified for secure (HTTPS) communication
    k = Klout('...', secure=True)
    # Optionally a timeout parameter (seconds) can also be sent with all calls
    score = k.user.score(kloutId=kloutId, timeout=5).get('score')

1 个答案:

答案 0 :(得分:0)

我添加了以下更改,它可以正常工作:

for cursor in collection.find().limit(10000):
    try:
        name =  cursor['user']['name']
        print name.encode('utf-8')
        kloutId = k.identity.klout(screenName=name).get('id')
        score = k.user.score(kloutId=kloutId).get('score')
        print "User's klout score is: %s" % (score)
        # By default all communication is not secure (HTTP). An optional secure parameter
        # can be sepcified for secure (HTTPS) communication
        k = Klout('...', secure=True)
        # Optionally a timeout parameter (seconds) can also be sent with all calls
        score = k.user.score(kloutId=kloutId, timeout=5).get('score')
        counter = counter+1
        except (KloutHTTPError, UnicodeEncodeError) as e:
                        print "Oops!  That was no kloudId found with that name, or unicode exception.  Try again... ", counter