0

Im stuck and in need of some help. I cant to seem to find any reference on how to download the file. Ive be using requests to find the file name but I dont know where to go from there.

'Content-disposition': 'attachment;filename=MT0376_DealerPrice.zip'

Any help would be greatly appreciated 

import requests
import csv
import urllib
from urllib.request import urlretrieve
import sys, os, base64, datetime, hashlib, hmac, urllib
from time import gmtime, strftime
import xml.etree.ElementTree as ET
import math
import time
import zipfile


dealerCode = 'Code'
userName ='User'
password = 'Pass'
loginUrl = 'https://www.lemansnet.com/login'

payload = {'rememberMe': 'on', 
           'dealerCode': dealerCode, 
           'dm': 4, 
           'userName': userName, 
           'password': password}


r = requests.post(loginUrl, params=payload)


token = r.headers['loginToken']


with open ("parts.xml", "r") as myfile:
    requestBody = myfile.read().replace('\n', '')

serviceURL = 'https://www.lemansnet.com/pricing/2013/pos'
ContentType = 'Content-Type:text/xml'
ContentLength = len(requestBody)
loginToken = 'loginToken:' + token

httparray = {'content': requestBody}

xml = """<pricing>
<whoForDealer>
<dealerCode>MT0376</dealerCode>
</whoForDealer>
<rememberPreferences>1</rememberPreferences>
</pricing>"""

params = {'http': httparray}

requestHeaders = {'Content-Type': 'text/xml',
                  'Content-Length': ContentLength, 
                  'charset': 'utf-8',
                  'loginToken': token,
                  'Cache-Control': 'no-cache',
                  'Pragma': 'no-cache',
                  'Connection': 'keep-alive'}
files ={'file': ('parts.xml')}


t = requests.post(serviceURL, data=xml, headers=requestHeaders)

#print(r.url)
#print(r.headers)
#print(r.text)
#print(r.status_code)
#print(r.content)
#print(t.url)
#print(t.headers)
#print(t.text)
print(t.status_code)
print(t.content)
with open("MT0376_DealerPrice1.zip", "wb") as code:
    code.write(t.content)
zip = t.headers['Content-disposition']
#print(zip)

#localName = zip.split('filename=')[1]
#print(localName)



print('end')

This is my current response from above code.

200
b'PK\x03\x04\x14\x00\x08\x08\x08\x00H\x8aOF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1b\x00\x00\x00PriceFile_system_errors.txt\xe3\xf5M\xac\xc8\xcc-\xcdUHI\xcc\xcc\xa9T\xc8\xc9\xcc\xcd,QH\xadHNMMIM\xd1\xe3\xc5+\x0b\x00PK\x07\x08\xcdO\x92K#\x00\x00\x00<\x00\x00\x00PK\x01\x02\x14\x00\x14\x00\x08\x08\x08\x00H\x8aOF\xcdO\x92K#\x00\x00\x00<\x00\x00\x00\x1b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00PriceFile_system_errors.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00I\x00\x00\x00l\x00\x00\x00\x00\x00'
end

Thanks for the help everyone, I think I managed to figure this one out. I edited the my code above to what I think is working correctly. Unfortunately the website im gathering my info from only allows me to download the file 3 times a day so I wont be able to really test it until tomorrow.

scate636
  • 135
  • 2
  • 11

1 Answers1

-1

The python built-in urllib.urlretrieve would be work:

import urllib

output_filename = '/tmp/my_downloaded_file.zip'
url = 'http://www.blog.pythonlibrary.org/wp-content/uploads/2012/06/'
url += 'wxDbViewer.zip'
# download...
urllib.urlretrieve(url, output_filename)

In python 3:

from urllib.request import urlretrieve
...
urlretrieve(url, output_filename)
felipsmartins
  • 13,269
  • 4
  • 48
  • 56