I do see a few methods of downloading a file from HTTP/HTTPS in Python, but for all of these you need to know the exact URL. I’m trying to download from a web service and the URL has methods and post arguments that are sent in order to download the file, I can’t figure out what the URL is to send. This is the code snippet:
url = 'https://www.example123.com'
params = { 'user' : 'username', 'pass' : 'password', 'method' : 'getproject', 'getPDF' : 'true' }
data = urllib.parse.urlencode(params)
data = data.encode('utf-8')
request= urllib.request.Request(url, data)
response = urllib.request.urlopen(request)
xdata = response.read()
print(xdata)
The print statement looks as though it’s reading the PDF, but I want to save it somewhere and can’t find any way to do that? Here is the beginning of the print response:
b'%PDF-1.6\r%\xe2\xe3\xcf\xd3\r\n12 0 obj\r<</Lin
You have to open a file and write to it. Right now, you are just storing it in a string variable.