I found an Excel/VBA script here and am trying to translate it to Python. First is the VBA, then my Python attempt. How should I fix the Python code?
Public Function GetUPSDeliveryDate(ByVal id As String) As String
Dim body As String, json As Object
body = "{""Locale"":""en_US"",""TrackingNumber"":[""" & id & """]}"
With CreateObject("MSXML2.XMLHTTP")
.Open "POST", "https://www.ups.com/track/api/Track/GetStatus?loc=en_US", False
.setRequestHeader "Referer", "https://www.ups.com/track?loc=en_US&requester=ST/"
.setRequestHeader "User-Agent", "Mozilla/5.0"
.setRequestHeader "DNT", "1"
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json, text/plain, */*"
.send body
Set json = JsonConverter.ParseJson(.responseText)
End With
Excel VBA/JSON to scrape UPS tracking delivery
My attempt is:
def f(tn):
data = {"Locale":"en_US",
"TrackingNumber":f"[{tn}]",
"Referer": "https://www.ups.com/track?loc=en_US&requester=ST/",
"User-Agent": "Mozilla/5.0",
"DNT": "1",
"Content-Type": "application/json",
"Accept": "application/json, text/plain, */*"}
r = requests.post("https://www.ups.com/track/api/Track/GetStatus?loc=en_US", data=data)
print(r.json())
My guess is one problem is putting the body portion from VBA into data.