urllib.request- HTTPError:HTTP错误403:禁止

时间:2019-03-05 15:47:38

标签: python api csv http urllib

我正在尝试使用Python自动从Octopart下载特定零件价格和数量的零件。我可以将要查找的特定零件号的csv文件转换为行项目和查询,但是当我尝试将查询发送到REST API进行零件匹配时,始终会收到HTTPError消息。我输入了apikey,但是由于它仍然无法连接,所以我想知道我是否错误地输入了url。任何指导将不胜感激。

代码:

 # Send queries to REST API for part matching.
import json
import urllib.parse
import urllib.request

results = []
for i in range(0, len(queries), 20):
# Batch queries in groups of 20, query limit of
# parts match endpoint
batched_queries = queries[i: i + 20]

url = 'http://octopart.com/api/v3/parts/match?queries=%s' \
    % urllib.parse.quote(json.dumps(batched_queries))
url += '&apikey=eb49732b'
data = urllib.request.urlopen(url)
response = json.loads(data)

# Record results for analysis
results.extend(response['results'])

错误:

    HTTPError                                 Traceback (most recent call last)
<ipython-input-43-cf5776fdc754> in <module>()
     14     url = 'http://octopart.com/api/v3/parts/match?queries=%s'         % urllib.parse.quote(json.dumps(batched_queries))
     15     url += '&apikey=eb49732b'
---> 16     data = urllib.request.urlopen(url)
     17     response = json.loads(data)
     18 

~\Documents\Software\Anaconda\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    220     else:
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 
    224 def install_opener(opener):

~\Documents\Software\Anaconda\lib\urllib\request.py in open(self, fullurl, data, timeout)
    529         for processor in self.process_response.get(protocol, []):
    530             meth = getattr(processor, meth_name)
--> 531             response = meth(req, response)
    532 
    533         return response

~\Documents\Software\Anaconda\lib\urllib\request.py in http_response(self, request, response)
    639         if not (200 <= code < 300):
    640             response = self.parent.error(
--> 641                 'http', request, response, code, msg, hdrs)
    642 
    643         return response

~\Documents\Software\Anaconda\lib\urllib\request.py in error(self, proto, *args)
    567         if http_err:
    568             args = (dict, 'default', 'http_error_default') + orig_args
--> 569             return self._call_chain(*args)
    570 
    571 # XXX probably also want an abstract factory that knows when it makes

~\Documents\Software\Anaconda\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
    501         for handler in handlers:
    502             func = getattr(handler, meth_name)
--> 503             result = func(*args)
    504             if result is not None:
    505                 return result

~\Documents\Software\Anaconda\lib\urllib\request.py in http_error_default(self, req, fp, code, msg, hdrs)
    647 class HTTPDefaultErrorHandler(BaseHandler):
    648     def http_error_default(self, req, fp, code, msg, hdrs):
--> 649         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    650 
    651 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 403: Forbidden

谢谢您的帮助!

2 个答案:

答案 0 :(得分:0)

检查您的API密钥或与他们联系并询问权限

当我尝试使用您的密钥对curl样本进行测试时,它也失败并显示403

$  curl -G https://octopart.com/api/v3/parts/match -d queries="[{\"mpn\":\"SN74S74N\"}]" \
-d apikey=eb49732b \
-d pretty_print=true
{
  "__class__": "ClientErrorResponse",
  "message": "Forbidden request"
}

但是,使用EXAMPLE_KEY进行的查询成功

答案 1 :(得分:0)

使用api密钥尝试以下代码...如果不起作用,则您的密钥可能无效。

import json
import urllib
import urllib.parse
import urllib.request

queries = [
    {'mpn': 'SN74S74N',
     'reference': 'line1'},
    {'sku': '67K1122',
     'reference': 'line2'},
    {'mpn_or_sku': 'SN74S74N',
     'reference': 'line3'},
    {'brand': 'Texas Instruments',
     'mpn': 'SN74S74N',
     'reference': 'line4'}
    ]

url = 'http://octopart.com/api/v3/parts/match?queries=%s' \
    % urllib.parse.quote(json.dumps(queries))
url += "&include[]=specs"

# NOTE: Use your API key here (https://octopart.com/api/register)
url += '&apikey=<REPLACEME>'

data = urllib.request.urlopen(url).read()
response = json.loads(data)

# print request time (in milliseconds)
print("Response time: %s msec\n" % response['msec'])