首先,我应该补充一点,您可以通过执行以下操作找到此请求:
1- Go to [airline site][1]
2- Type in "From" = "syd"
3- Type in "To" = "sin"
4- Make the departure date sep.3 and click one-way and search
5- On the search result page check your network get request when you click on an available seat option radio button
我正在尝试使用请求模块来获取响应,例如来自this site
这就是我正在尝试的事情:
url = "http://www.singaporeair.com/chooseFlightJson.form?"
payload = {'selectedFlightIdDetails[0]':amount_data,'hid_flightIDs':'','hid_recommIDs':'','tripType':"O",'userPreferedCurrency':""}
response = requests.get(url, params=payload)
print response.json()
响应应该是:
{"price":{"total":"595.34","currency":{"code":"AUD","label":""},"adult":{"count":1,"label":"Adult","cost":"328.00","total":"328.00"},"child":{"count":0,"label":"Child","cost":"0.00","total":"0.00"},"infant":{"count":0,"label":"Infant","cost":"0.00","total":"0.00"},"addOns":[{"label":"Airport / Government taxes ","cost":"83.24"},{"label":"Carrier Surcharges","cost":"184.10"}],"disclaimer":"Prices are shown in Canadian Dollar(CAD)","rate":"595.34 AUD \u003d 913.80 CAD","ratehint":"Estimated","unFormattedTotal":"595.34"},"messages":{"O3FF11SYD":"A few seats left","O1FF31SYD":" ","R0FF31SYD":"A few seats left","O2FF31SYD":"A few seats left","O0FF31SYD":" ","O1FF11SYD":"A few seats left","O0FF21SYD":" ","O2FF21SYD":" ","O3FF21SYD":" ","O1FF21SYD":" "},"cabinClass":{"onwardCabin":["Economy"]}}
答案 0 :(得分:2)
响应 值None
,以JSON编码;服务器返回null\r\n
,这与Python中的None
相同。
这里的内容类型不对;它设置为text / html,但response.json()
返回值对于服务器发送的内容完全正确:
>>> import requests
>>> url = "http://www.singaporeair.com/chooseFlightJson.form?"
>>> amount_data = 0
>>> payload = {'selectedFlightIdDetails[0]':amount_data,'hid_flightIDs':'','hid_recommIDs':'','tripType':"O",'userPreferedCurrency':""}
>>> response = requests.get(url, params=payload)
>>> response
<Response [200]>
>>> response.headers['content-type']
'text/html; charset=ISO-8859-1'
>>> response.text
'null\r\n'
>>> response.json() is None
True
答案 1 :(得分:0)
解决方案是使用请求会话,如下所示:
session = requests.Session()
然后调用所需的所有网址:
response = session.get(url)
这设置了检索数据所必需的cookie和会话变量。 我已经看到jsessionid以不同的方式使用,在url中,或者在这种情况下在cookie中使用。但是可能还需要其他会话信息,这些信息由请求会话对象处理。
答案 2 :(得分:0)
在发出 http 请求时,请确保 response_type 设置为您正在尝试的确切用例。 就我而言,response_type='object' 致力于消除响应中的 None 类型。