Python3 连接被同行重置

Python3 Connection reset by peer

urllib.error.URLError urlopen error [Errno 54] Connection reset by peer

我在尝试获取 notino.com 时收到此错误。我猜这家伙用了一些聪明的方法来阻止屏幕 scraper 。我尝试添加 header 和 cookie,但这不起作用

from urllib.request import urlopen
url = "https://www.notino.com"
html = urlopen(url)

auto-bot 检测机制很可能会断开您的连接。您应该提供 User-Agent header 来伪造浏览器访问 - 对我有用:

>>> import requests
>>> response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'})
>>> response.status_code
200

在此示例中使用 requests module