【问题标题】:Python requests 403 Forbidden referer from network headersPython 从网络标头请求 403 Forbidden referer
【发布时间】:2021-05-07 02:48:57
【问题描述】:

这个请求曾经有效,但现在得到了 403。我尝试添加一个像这个答案一样的用户代理,但仍然不行:https://stackoverflow.com/a/38489588/2415706

第二个答案进一步说要找到引用标头,但我无法弄清楚这些响应标头在哪里:https://stackoverflow.com/a/56946001/2415706

import requests
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
"referer": "https://www.ziprecruiter.com/Salaries/What-Is-the-Average-Programmer-Salary-by-State"
job_url = "https://ziprecruiter.com/Salaries/What-Is-the-Average-Programmer-Salary-by-State"
job_response = requests.get(job_url,  headers=headers, timeout=10)
print(job_response)

这是刷新页面后我在第一个选项卡的请求标头下看到的内容,但内容太多。我想我只需要这些行之一。

:authority: www.ziprecruiter.com
:method: GET
:path: /Salaries/What-Is-the-Average-Programmer-Salary-by-State
:scheme: https
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
cache-control: max-age=0
cookie: __cfduid=dea4372c39465cfa2422e97f84dea45fb1620355067; zva=100000000%3Bvid%3AYJSn-w3tCu9yJwJx; ziprecruiter_browser=99.31.211.77_1620355067_495865399; SAFESAVE_TOKEN=1a7e5e90-60de-494d-9af5-6efdab7ade45; zglobalid=b96f3b99-1bed-4b7c-a36f-37f2d16c99f4.62fd155f2bee.6094a7fb; ziprecruiter_session=66052203cea2bf6afa7e45cae7d1b0fe; experian_campaign_visited=1
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"
sec-ch-ua-mobile: ?0
sec-fetch-dest: document
sec-fetch-mode: navigate
sec-fetch-site: none
sec-fetch-user: ?1
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36

编辑:查看其他选项卡,他们有推荐人:“推荐人”:“https://www.ziprecruiter.com/Salaries/What-Is-the-Average-Programmer-Salary-by-State”所以我我现在正在尝试,但它仍然是 403。

【问题讨论】:

    标签: web-scraping python-requests http-status-code-403


    【解决方案1】:

    使用httpx package 似乎可以使用:

    import httpx
    
    url = 'https://ziprecruiter.com/Salaries/What-Is-the-Average-Programmer-Salary-by-State'
    
    r = httpx.get(url)
    
    print(r.text)
    print(r.status_code)
    print(r.http_version)
    

    repl.it:https://replit.com/@bertrandmartel/ZipRecruiter

    我可能错了,但我认为服务器不喜欢请求库的 TLS 协商。这很奇怪,因为上面的调用在请求中使用了 HTTP1.1,而 curl 它只适用于 http2 和 TLS1.3

    使用使用 http2 和支持 TLS1.3 的 openssl 构建的 curl 二进制文件,以下工作:

    docker run --rm curlimages/curl:7.76.1 \
        --http2 --tlsv1.3 'https://ziprecruiter.com/Salaries/What-Is-the-Average-Programmer-Salary-by-State' \
        -H 'user-agent: Mozilla' \
        -s -o /dev/null -w "%{http_code}"
    

    返回:

    301
    

    以下命令失败:

    • 强制使用 http1.1 并强制使用 TLS 1.3
    docker run --rm curlimages/curl:7.76.1 \
        --http1.1 --tlsv1.3 'https://ziprecruiter.com/Salaries/What-Is-the-Average-Programmer-Salary-by-State' \
        -H 'user-agent: Mozilla' \
        -s -o /dev/null -w "%{http_code}"
    

    输出:403

    • 强制使用 http2 并强制使用 TLS 1.2:
    docker run --rm curlimages/curl:7.76.1 \
        --http2 --tlsv1.2 'https://ziprecruiter.com/Salaries/What-Is-the-Average-Programmer-Salary-by-State' \
        -H 'user-agent: Mozilla' \
        -s -o /dev/null -w "%{http_code}"
    

    输出:403

    我的猜测是它在 TLS 协商中检测到某些东西,但是当同时存在 TLS1.3 和 HTTP/2 时检查是不同的

    很遗憾,您无法使用 requests/urlib 检查 http/2,因为它不受支持

    【讨论】:

    • 更可能是 cloudflare 还没有看到足够的 httpx 流量来识别/阻止它。
    • 谢谢你,它有效,现在我可以取消放弃该项目了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 2013-08-21
    • 2018-07-15
    • 2016-09-24
    • 2018-04-14
    相关资源
    最近更新 更多