2014-05-19 54 views
1

我已經安裝https://github.com/muhuk/python-awis以從alexa獲取URL信息。Python Awis API不提供IOError

我用這

import awis 
api = AwisApi(ACCESS_ID, SECRET_ACCESS_KEY) 
tree = api.url_info(("example1.com", "example2.com", "example3.com"), "Rank", "LinksInCount") 
elem = tree.find("//{%s}StatusCode" % api.NS_PREFIXES["alexa"]) 
assert eenter code herelem.text == "Success" 

當我嘗試運行該腳本,我得到這個錯誤:

Traceback (most recent call last): 
    File "C:\Users\John\Desktop\Python Scripts\Domains\alexa api\AlexaURL.py", line 3, in <module> 
    tree = api.url_info(("example1.com", "example2.com", "example3.com"), "Rank", "LinksInCount") 
    File "C:\Users\John\Desktop\Python Scripts\Domains\alexa api\awis\__init__.py", line 121, in url_info 
[Finished in 0.5s with exit code 1] return self.request(params, **kwargs) 
    File "C:\Users\John\Desktop\Python Scripts\Domains\alexa api\awis\__init__.py", line 91, in request 
    response = urllib.urlopen(url) 
    File "C:\python27\lib\urllib.py", line 87, in urlopen 
    return opener.open(url) 
    File "C:\python27\lib\urllib.py", line 208, in open 
    return getattr(self, name)(url) 
    File "C:\python27\lib\urllib.py", line 359, in open_http 
    return self.http_error(url, fp, errcode, errmsg, headers) 
    File "C:\python27\lib\urllib.py", line 372, in http_error 
    result = method(url, fp, errcode, errmsg, headers) 
    File "C:\python27\lib\urllib.py", line 683, in http_error_401 
    errcode, errmsg, headers) 
    File "C:\python27\lib\urllib.py", line 381, in http_error_default 
    raise IOError, ('http error', errcode, errmsg, headers) 
IOError: ('http error', 401, 'Unauthorized', <httplib.HTTPMessage instance at 0x022FD580>) 

任何想法?

回答

0

您收到的回覆是「401未授權」,這可能意味着您的ACCESS_ID和/或SECRET_ACCESS_KEY不正確。

此外,請確保您將上述兩個鍵作爲字節傳遞,而不是字符串,否則您將在該處發生錯誤。

可以使用str.encode功能,像:

api = awis.AwisApi(str.encode(self.access_key_id), 
          str.encode(self.secret_access_key_id)) 

否則你的腳本是正確的。我使用它就像這樣,它的工作原理。

+0

謝謝我會試試這個。快速問題你是什麼意思,確保我將它們作爲字節傳遞,你有沒有例子? – mofoparrot

+0

@mofoparrot我用你可以使用的代碼示例編輯答案。希望能幫助到你。 –

+0

@NikosSteiakakis我不斷收到此響應IOError:所有3個請求失敗,最新的響應代碼是403 – ashim888