我正在用python 3开发一个简单的web scraper,但当我发送get或post请求时,响应是403.在python中,2可以很好地工作.我在两个版本中使用了相同版本的请求库.我有

请求数=2.22.0

certifi=2019.9.11

from requests import get
url = 'https://www.gamestop.com/'
header = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-US,en;q=0.5',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0',
    'DNT': '1',
    'Upgrade-Insecure-Requests': '1',
    'Connection': 'keep-alive',
    'Host': 'www.gamestop.com'
}
res = get(url, headers=header, verify=False).status_code
print(res)
# 403 when using python 3.7.4
# 200 when using python 2.7.16

Edit by @blhsing:

下面的列表根据注释记录了哪些特定的Python版本有效,哪些版本失败.到目前为止,跨平台的每个特定Python版本的成功和失败都是一致的.

请使用您自己的结果以及用于生成结果的特定Python版本,随意编辑问题的这一部分.

2.7.14 works (blhsing)
2.7.16 works (repl.it)
3.6.5 works (blhsing)
3.6.8 fails (Reinderien and blhsing)
3.7.3 works (wim and blhsing)
3.7.4 fails (repl.it and blhsing)
3.8.0 fails (OP)

repl上的演示.它:Python 2.7.16Python 3.7.4

推荐答案

这是urlib3引发的异常:

/本垒/亚军/.local/share/virtualenvs/python3/lib/python3.7/站点包/urllib3/connectionpool.py:https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings4:

根据latest release notes, section 1.25.5 (2019-09-19)人的说法:

增加BPO-37428 affecting Python <3.7.4 and OpenSSL的缓解措施

你可以跟着issue on Github号,它已经关闭了.

TLDR

User @sethmlarson on Githuburllib3上发现了这个错误:

创建_urlib3_context():

    # Enable post-handshake authentication for TLS 1.3, see GH #1634. PHA is
    # necessary for conditional client cert authentication with TLS 1.3.
    # The attribute is None for OpenSSL <= 1.1.0 or does not exist in older
    # versions of Python.
    if getattr(context, "post_handshake_auth", None) is not None:
        context.post_handshake_auth = True

将此值设置为True将启用服务器证书验证,而不是禁用.

Python-3.x相关问答推荐

根据其他数据框架的列顺序从数据框架中进行 Select

使用Python装载. iso文件

使用PANAS根据另两个列表中的值对一个列表中的字符串值进行分组

被多个\n拆分并保留

我用Kivy创建的应用程序在安卓系统上运行时出错.(attributeerror:';class';对象没有属性';_javaclass__cls_storage';)

如何在 python 中将带有时区信息的时间戳转换为 utc 时间

我想使用命令提示符安装 cv2

在 string.find() 条件下加入两个 Dataframes

pytorch 中 mps 设备的 manual_seed

段落中句子的索引

在 Django 中执行 JSONRenderer.render(serialized_student_data.data) 时遇到问题

如何准确测定cv2的结果.在BW/黑白图像中查找对象?

排队多个子进程

将变量传递给 Google Cloud 函数

获取比较多列的最大值并返回特定值

在两个数据框之间查找相等的列

具有两个或多个返回参数的函数注释

在计算之前删除包含某些值的组合

Tensorflow:ImportError:libcudnn.so.7:无法打开共享对象文件:没有这样的文件或目录

Python - 类 __hash__ 方法和集合