urllib:打开一个url总是得到429:请求太多了

问题描述 投票:0回答:1

我刚开始使用urllib模块。我正试图从超市中榨取产品,并且有一个似乎总是以HTTP Error 429: Too many requests回应的网站。我已经对Stack Overflow做了一些研究,似乎没有人遇到同样的问题。我的代码非常简单:

>>> import urllib.request
>>> resp = urllib.request.urlopen("https://shop.coles.com.au/a/a-national/product/head-shoulders-shampoo-conditioner-2in1-deep-clean")
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    resp = urllib.request.urlopen("https://shop.coles.com.au/a/a-national/product/head-shoulders-shampoo-conditioner-2in1-deep-clean")
  File "C:\Users\thank\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
  File "C:\Users\thank\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open
response = meth(req, response)
  File "C:\Users\thank\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 640, in http_response
'http', request, response, code, msg, hdrs)
  File "C:\Users\thank\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 568, in error
return self._call_chain(*args)
  File "C:\Users\thank\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
  File "C:\Users\thank\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 648, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 429: Too Many Requests

我也试图像this answer建议的那样修改用户代理,但结果仍然是一样的

有人可以解释urllib模块中的哪些默认设置可能导致问题?或者是因为网站阻止机器人?该网站的其他产品页面也不起作用。

python urllib http-status-code-429
1个回答
1
投票

429是服务器要求你停止。基本上,Web服务器认为您正在尝试垃圾邮件或抓取它并且它不喜欢它。一般来说,您应该尊重服务器,如果在一段时间后尝试429响应,您应该遵循它。

如果您觉得服务器错误地询问了您,则可以确保您的用户请求与用户从浏览器生成的用户请求类似,其中包括用户代理和所有其他信息a常规浏览器会随请求一起发送。如果服务器发送给你429,尽管很可能它暂时或永久阻止了你的ip。你应该看看如何刮掉多个ips。

© www.soinside.com 2019 - 2024. All rights reserved.