如何从PYTHON脚本的POST请求中获取正确的JSON响应

时间:2019-01-16 06:45:53

标签: python json python-3.x post python-requests

任务是从特定网站的POST请求中获取JSON响应。

在浏览器中,一切正常如下。您可以自己尝试尝试模拟这种情况,以在“开始位置”字段中开始输入文本。

要检查的网址:https://www.hapag-lloyd.com/en/online-business/schedules/interactive-schedule.html

Chrome Dev Tool Screen 1 - Request URL and Header

Chrome Dev Tool Screen 2 - POST data

JSON响应(必须是这样)

{"rows":[{"LOCATION_COUNTRYABBREV":"GE","LOCATION_BUSINESSPOSTALCODE":"","LOCATION_BUSINESSLOCATIONNAME":"BATUMI","LOCATION_BUSINESSLOCODE":"GEBUS","STANDARDLOCATION_BUSINESSLOCODE":"GEBUS","LOCATION_PORTTYPE":"S","DISPLAYNAME":""}]}

我的代码如下:

import requests

url = 'https://www.hapag-lloyd.com/en/online-business/schedules/interactive-schedule.html?_sschedules_interactive=_raction&action=getTypeAheadService'

POST_QUERY = 'batumi'

params = {
    'query': POST_QUERY,
    'reportname': 'FRTA0101',
    'callConfiguration': "[resultLines=10,readDef1=location_businessLocationName STARTSWITH,readDef2=location_businessLocode STARTSWITH,readClause1=location_businessLocode<>'' AND location_portType='S' AND stdSubLocation_string10='STD',readClause2=location_businessLocode<>'' AND location_portType<>'S' AND stdSubLocation_string10='STD',readClause3=location_businessLocode<>'' AND location_portType='S' AND stdSubLocation_string10='SUB',readClause4=location_businessLocode<>'' AND stdSubLocation_string10='SUB',readClause5=location_businessLocode='' AND stdSubLocation_string10='SUB',sortDef1=location_businessLocationName ASC,resultAttr1=location_businessLocationName,resultAttr2=location_businessLocode,resultAttr3=location_businessPostalCode,resultAttr4=standardLocation_businessLocode,resultAttr5=location_countryAbbrev,resultAttr6=location_portType]"
}
headers = {
    "Accept": "*/*",
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'en-EN,en;q=0.9,en-US;q=0.8,en;q=0.7',
    'Cache-Control': 'no-cache',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'DNT': '1',
    'Host': 'www.hapag-lloyd.com',
    'Origin': 'https://www.hapag-lloyd.com',
    'Pragma': 'no-cache',
    # 'Proxy-Connection': 'keep-alive',
    'Referer': 'https://www.hapag-lloyd.com/en/online-business/schedules/interactive-schedule.html',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}

print('Testing location: ', POST_QUERY)
var_cities = requests.post(url,data=params,headers=headers)
print(var_cities.content) #it does print some %$#%$

Python Print Content Screen

我的问题是“如何从PYTHON脚本的POST请求中获取正确的JSON响应”?

2 个答案:

答案 0 :(得分:0)

我认为使用BeautifulSoup是更好的选择。 试试这个

Python Convert HTML into JSON using Soup

答案 1 :(得分:0)

print(var_cities.text)

这将html作为字符串返回。这是您期望得到的答复吗?并将其转换为json,请看上面的答案...