我无法从网站获得正确的响应:“不允许从此 URL 请求”

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

我倾向于使用皇家邮政网站来获取给定英国邮政编码的潜在地址: https://www.royalmail.com/rml-shared-find-a-postcode

为了节省时间,我的目标是使用 python requests 模块来模拟所有字段,就像浏览器使用检查工具并查看网络一样。 基本上,端点需要一个密钥,并且每天使用该密钥收到的请求限制为 50 个。

在初始阶段,我只是创建了一个非常简单的请求,以查看一切是否正常。它只是从我的浏览器的所述检查工具复制标头和有效负载。

所以我的Python代码是这样的:

import requests
import json


headers = {
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate, br, zstd',
    'Accept-Language': 'es-ES,es;q=0.7',
    'Origin': 'https://www.royalmail.com',
    'Referrer': 'https://www.royalmail.com/',
    'Sec-Ch-Ua': '"Brave";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
    'Sec-Ch-Ua-Mobile': '?0',
    'Sec-Ch-Ua-Platform': '"Windows"',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'cross-site',
    'Sec-Gpc': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'
    }


r = requests.get('https://services.postcodeanywhere.co.uk/Capture/Interactive/Find/v1.00/json3ex.ws?Key=FG74-CF73-XJ77-KR34&Text=RG10%200AB&Origin=GBR&Language=en&Container=&Filter=undefined&Instance=null&Test=false&$block=true&$cache=true', headers=headers)

r1 = r.json()

不幸的是,回应总是:

{'Items': [{'Error': '5', 'Description': 'Request not allowed from this URL', 'Cause': 'The request was disallowed from the URL.', 'Resolution': 'Check the security settings on the key first. If they look fine, please contact support as it may be from a URL on our blacklist.'}]}

浏览器中一切正常,但无法通过请求模块获得正确的响应。

我错过了什么?

web-scraping python-requests
1个回答
0
投票

其中一个标题中有拼写错误,从而导致了问题。

'Referrer': 'https://www.royalmail.com/'
应该是
Referer

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