Reputation: 3
I'm trying to write a python script to get the tracking info from this page: https://www.purolator.com/en/app-tracker.page. Using F12 in Chrome, I got the header and the request payload, no form data just request payload. The tracking number I'm trying is: GRH000241377
Here's my code:
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()
Response code is 200 but when i print the response.text i get the same page without any result, and if try to get response.json()
i got next error, I'm assuming that error is because i have no json in the response
'Error occurred : \n Error Message: Expecting value: line 1 column 1 (char 0)'
Presumably error is on data or header construction but can't figure out the problem. Any idea? Thanks
Here are the response headers:
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
Upvotes: 0
Views: 13119
Reputation: 340
I hope this helps.
Python code:
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)
Output:
{"trackingResults": [{"searchStatus": "Found", "foundBy": "Pin", "searchCriteria": {"trackingNumber": "GRH000241377", "type": "Unspecified", "sequenceID": 1}, ..., "isStateBehind": False}
Upvotes: 1