发送带有参数的 post 请求时脚本卡住

SMTH

我正在尝试使用来自网页的适当参数来填充 json 响应,并发出 post http 请求。当我运行脚本时,我看到脚本卡住了并且没有带来任何结果。它也不会抛出任何错误。这是站点链接在点击按钮之前,我从站点的此表单的三个下拉列表中选择了三个选项Get times & tickets

我试过:

import requests
from bs4 import BeautifulSoup

url = 'https://www.thetrainline.com/'
link = 'https://www.thetrainline.com/api/journey-search/'

payload = {"passengers":[{"dateOfBirth":"1991-01-31"}],"isEurope":False,"cards":[],"transitDefinitions":[{"direction":"outward","origin":"1f06fc66ccd7ea92ae4b0a550e4ddfd1","destination":"7c25e933fd14386745a7f49423969308","journeyDate":{"type":"departAfter","time":"2021-02-11T22:45:00"}}],"type":"single","maximumJourneys":4,"includeRealtime":True,"applyFareDiscounts":True}

with requests.Session() as s:
    s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'
    s.headers['content-type'] = 'application/json'
    s.headers['accept'] = 'application/json'
    r = s.post(link,json=payload)
    print(r.status_code)
    print(r.json())

我怎样才能得到 json 响应,发出带有来自该站点的参数的 post 请求?

服务

您缺少所需的标头:x-versionreferer. referer标题指的是搜索表单,您可以建立它。journey-search您必须发布availability请求之前。

import requests
from requests.models import PreparedRequest

headers = {
    'authority': 'www.thetrainline.com',
    'pragma': 'no-cache',
    'cache-control': 'no-cache',
    'x-version': '2.0.18186',
    'dnt': '1',
    'accept-language': 'en-GB',
    'sec-ch-ua-mobile': '?0',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/88.0.4324.96 Safari/537.36',
    'content-type': 'application/json',
    'accept': 'application/json',
    'origin': 'https://www.thetrainline.com',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-mode': 'cors',
    'sec-fetch-dest': 'empty',
}

with requests.Session() as s:
    origin = "6e2242b3f38bbbd8d8124e1d84d319e1"
    destination = "15bcf02bc44ea754837c8cf14569f608"
    localDateTime = "2021-02-03T19:30:00"
    dateOfBirth = "1991-02-03"
    passenger_type = "single"

    req = PreparedRequest()
    url = "http://www.neo4j.com"
    params = {
        "origin": origin,
        "destination": destination,
        "outwardDate": localDateTime,
        "outwardDateType": "departAfter",
        "journeySearchType": passenger_type,
        "passengers[]": dateOfBirth
    }
    req.prepare_url("https://www.thetrainline.com/book/results", params)

    headers.update({"referer": req.url})
    s.headers = headers

    payload_availability = {
        "origin": origin,
        "destination": destination,
        "outwardDefinition": {
            "localDateTime": localDateTime,
            "searchMethod": "DEPARTAFTER"
        },
        "passengerBirthDates": [{
            "id": "PASSENGER-0",
            "dateOfBirth": dateOfBirth
        }],
        "maximumNumberOfJourneys": 4,
        "discountCards": []
    }
    r = s.post('https://www.thetrainline.com/api/coaches/availability', json=payload_availability)
    r.raise_for_status()

    payload_search = {
        "passengers": [{"dateOfBirth": "1991-02-03"}],
        "isEurope": False,
        "cards": [],
        "transitDefinitions": [{
            "direction": "outward",
            "origin": origin,
            "destination": destination,
            "journeyDate": {
                "type": "departAfter",
                "time": localDateTime}
        }],
        "type": passenger_type,
        "maximumJourneys": 4,
        "includeRealtime": True,
        "applyFareDiscounts": True
    }
    r = s.post('https://www.thetrainline.com/api/journey-search/', json=payload_search)
    r.raise_for_status()

    print(r.json())

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ios发送带有POST参数的POST请求

带有axios的POST请求未发送参数

如何使用cURL发送带有请求参数的POST请求?

使用 J Meter 使用 Web-kit Form-Data 发送带有 POST 请求的参数

在Python中,如何发送带有5个嵌套深的URL编码参数的POST请求?

如何通过带有标题中的自定义参数的RestTemplate发送POST请求

发送带有数据的POST请求时出错

无法发送带有androidannotations的POST参数

如何使用Alamofire 4在Swift 3中发送带有参数和正文的JSON数据的POST请求?

Android Volley:服务器上接收到带有POST请求发送的String参数的空值

Angular:发送带有空请求正文的POST请求

使用request.post发送带有证书的请求

使用Volley发送带有JSON数据的POST请求

使用Guzzle发送带有JSON的POST请求

发送带有特殊字符的POST请求iOS

将带有 POST 请求的文件发送到 TelegramBot

使用Guzzlehttp发送带有JSON正文的POST请求

在R中发送带有正文的POST请求

改进:如何发送带有常量字段的POST请求?

如何发送带有表中数据的POST请求?

在axios中发送带有正文的POST请求

Http Post发送带有空正文的请求

如何使用Volley发送带有JSON正文的POST请求?

如何快速发送带有嵌套字典的POST请求

最好发送1个带有数组参数的POST请求或40个POST请求以在DB中创建40个对象?

Angular 5:将带有字符串参数和FormData参数的POST请求一起发送到Spring REST Controller

尝试发送POST请求时,应用脚本发送405响应

如何在带有多个参数的angularjs中发送POST?

如何在 AngularJS 中发送带有多个参数的 POST