2014-05-19 77 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