从klout异常KloutHTTPE检索数据

2024-10-01 13:42:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用kloud python API检索klouID。我正在从mongo数据库中获取用户名,并尝试使用此用户名检索kloudid。我注意到我只能为一些用户检索id(他们或他们的一些追随者在klout注册)。因此,我想创建一个try-except,以克服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')

Tags: nameidhttpforgetcursor用户名score
1条回答
网友
1楼 · 发布于 2024-10-01 13:42:46

我添加了以下更改,效果很好:

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

相关问题 更多 >