首页 > 解决方案 > urllib.request- HTTPError:HTTP 错误 403:禁止

问题描述

我正在尝试使用 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

谢谢您的帮助!

标签: pythonapicsvhttpurllib

解决方案


使用您的 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'])

推荐阅读