python 请求给出 'None' 响应,其中需要 json 数据
python requests gives 'None' response, where json data is expected
首先,我要补充一点,您可以通过执行以下操作找到此请求:
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"]}}
响应 是 值 None
,编码在 JSON 中;服务器 returns null\r\n
,这与 Python 中的 None
相同。
此处内容类型错误;它设置为 text/html,但是 response.json()
return 值对于服务器发送的内容是完全正确的:
>>> 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
解决方案是使用请求会话,如下所示:
session = requests.Session()
然后调用所有 urls 你需要简单地做:
response = session.get(url)
这会设置检索数据所需的 cookie 和会话变量。
我已经看到 jsessionid 以不同的方式使用,在 url 中,或者在本例中是在 cookie 中。但是可能还需要其他会话信息,这些信息由请求会话对象处理。
在发出 http 请求时,请确保 response_type 设置为您正在尝试的确切用例。
在我的例子中,response_type='object' 努力消除 None 类型的响应。
首先,我要补充一点,您可以通过执行以下操作找到此请求:
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"]}}
响应 是 值 None
,编码在 JSON 中;服务器 returns null\r\n
,这与 Python 中的 None
相同。
此处内容类型错误;它设置为 text/html,但是 response.json()
return 值对于服务器发送的内容是完全正确的:
>>> 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
解决方案是使用请求会话,如下所示:
session = requests.Session()
然后调用所有 urls 你需要简单地做:
response = session.get(url)
这会设置检索数据所需的 cookie 和会话变量。 我已经看到 jsessionid 以不同的方式使用,在 url 中,或者在本例中是在 cookie 中。但是可能还需要其他会话信息,这些信息由请求会话对象处理。
在发出 http 请求时,请确保 response_type 设置为您正在尝试的确切用例。 在我的例子中,response_type='object' 努力消除 None 类型的响应。