我正在尝试编写python脚本以从以下页面获取跟踪信息:https://www.purolator.com/en/app-tracker.page。 在Chrome中使用F12,我获得了标头和请求有效负载,没有表单数据只是请求有效负载。我正在尝试的跟踪号码是:GRH000241377
这是我的代码:
header = {
'authority': 'api.purolator.com',
'method': 'POST',
'path': '/tracker/puro/json/shipment/search',
'scheme': 'https',
'accept': 'application/vnd.puro.shipment.trackingevent+json',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-CA',
'content-length': '90',
'content-type': 'application/vnd.puro.shipment.trackingevent+json',
'origin': 'https://www.purolator.com',
'referer': 'https://www.purolator.com/en/app-tracker.page?pin=GRH000241377&sdate=2019-12-09',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/79.0.3945.117 Safari/537.36'
}
data = {
'pins':'[{"pin":"GRH000241377","sequenceID":1}]',
'searchOptions':'{"includeReference":true}'
}
url = "https://www.purolator.com/en/app-tracker.page"
response = requests.post(url, headers=header, json=data)
res_json = response.json()
响应代码为200,但是当我打印response.text时,我得到的页面却没有任何结果,并且如果尝试获取response.json()
时出现下一个错误,我假设该错误是因为我没有响应中的json
'Error occurred : \n Error Message: Expecting value: line 1 column 1 (char 0)'
大概是由于数据或标头构造上的错误,但无法找出问题所在。 任何想法? 谢谢
以下是响应标头:
access-control-allow-origin: https://www.purolator.com
content-length: 1102
content-security-policy: default-src 'self' *.hs-cpggpc.ca *.canadapost.ca *.cpggpc.ca *.purolator.com *.epost.ca
content-type: application/vnd.puro.shipment+json
date: Tue, 14 Jan 2020 00:37:38 GMT
status: 200
strict-transport-security: max-age=86400; preload
svn-build-number: Purolator.buildLevel.20190622.BF_2019A-B15568.15568
via: 1.1 199fd61d7551d8868317c5b53cc7d24d.cloudfront.net (CloudFront)
x-amz-apigw-id: GQ8E3HOV4osFhyQ=
x-amz-cf-id: Yqyt1uuT9NuucZiYP7arFLAFnTdKbK8yAfRrJ5nfnV7kUyNbhXH0OQ==
x-amz-cf-pop: IAD89-C3
x-amzn-remapped-connection: keep-alive
x-amzn-remapped-content-length: 1102
x-amzn-remapped-date: Tue, 14 Jan 2020 00:37:38 GMT
x-amzn-requestid: a04f8174-fc03-49f5-864c-23bb76ff4434
x-backside-transport: OK OK,OK OK,OK OK,OK OK
x-cache: Miss from cloudfront
x-content-type-options: nosniff
x-frame-options: ALLOW-FROM https://https://www.purolator.com
x-xss-protection: 1; mode=block
答案 0 :(得分:0)
我希望这会有所帮助。
Python代码:
import requests
header = {
'authority': 'api.purolator.com',
'method': 'POST',
'path': '/tracker/puro/json/shipment/search',
'scheme': 'https',
'accept': 'application/vnd.puro.shipment.trackingevent+json',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-CA',
'content-length': '90',
'content-type': 'application/vnd.puro.shipment.trackingevent+json',
'origin': 'https://www.purolator.com',
'referer': 'https://www.purolator.com/en/app-tracker.page?pin=GRH000241377&sdate=2019-12-09',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'
}
data = {"trackingNumbers":[{"trackingNumber":"GRH000241377", "type":"Unspecified","sequenceID":1}]}
url = "https://api.purolator.com/tracker/puro/json/shipment/trackingEvent/summary/search"
response = requests.post(url, headers=header, json=data)
res_json = response.json()
print (res_json)
输出:
{"trackingResults": [{"searchStatus": "Found", "foundBy": "Pin", "searchCriteria": {"trackingNumber": "GRH000241377", "type": "Unspecified", "sequenceID": 1}, ..., "isStateBehind": False}