Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with CSV download
#1
Hey there - very very new to Python and trying to knock up a script that posts to an API.
The core of the script works fine and I can successfully post to an API, but the API returns a csv, not to be confused with a CSV URI. It's identical to the web browser download once the csv is ready.

The issue is that Python seems to be trying to read the download into memory, not the actual CSV file. Does that make sense?

The code I'm firing is this..

exporturl = "https://website.com/Inquiries/ExportToCsv"

exportpayload = 'searchOptions={"filmCode":"","From":"10/11/2018","To":"11/11/2018","TransactionNumber":"","SessionFrom":"","SessionTo":"","TransactionType":"All","BookingReference":"","itemCode":"","ticketCode":"","userCode":"","workstationCode":"","CardNumberFirstSixDigits":"","CardNumberLastFourDigits":"","BookingEmail":"","MembershipCardNumber":"","PaymentType":"","BookingName":""}'

exportheaders = {
'origin': "https://website.com",
'upgrade-insecure-requests': "1",
'content-type': "application/x-www-form-urlencoded",
'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
'referer': "https://website.com/inquiries/transactions",
'accept-encoding': "gzip, deflate, br",
'accept-language': "en-US,en;q=0.9,es;q=0.8",
'cache-control': "no-cache",
}


response = session.post(exporturl, cookies=session.cookies.get_dict(), headers=exportheaders, data=exportpayload, verify=False)

Python waits for the response and then just displays garbage - which is basically the attempted download of the csv file.
If I run the exact same thing via Postman it works fine and the file is downloaded via the web browser in the background.

Any pointers would be great. I've tried to write out the data to file, which generates a file but it's the same garbage which would be displayed if I wrote it to console.

Thanks
Immy
Python 3.7
Reply
#2
This should make it easier to read
exporturl = "https://website.com/Inquiries/ExportToCsv"

exportpayload = 'searchOptions={"filmCode":"","From":"10/11/2018","To":"11/11/2018","TransactionNumber":"","SessionFrom":"","SessionTo":"","TransactionType":"All","BookingReference":"","itemCode":"","ticketCode":"","userCode":"","workstationCode":"","CardNumberFirstSixDigits":"","CardNumberLastFourDigits":"","BookingEmail":"","MembershipCardNumber":"","PaymentType":"","BookingName":""}'

exportheaders = {
'origin': "https://website.com",
'upgrade-insecure-requests': "1",
'content-type': "application/x-www-form-urlencoded",
'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
'referer': "https://website.com/inquiries/transactions",
'accept-encoding': "gzip, deflate, br",
'accept-language': "en-US,en;q=0.9,es;q=0.8",
'cache-control': "no-cache",
}


response = session.post(exporturl, cookies=session.cookies.get_dict(), headers=exportheaders, data=exportpayload, verify=False)
Reply
#3
(Nov-11-2018, 09:17 PM)imtiazu Wrote: Python waits for the response and then just displays garbage - which is basically the attempted download of the csv file.
That garbage can be binary data of csv file,so try to save content.
import requests

with open("data.csv",'wb') as f_out:
    f_out.write(response.content)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  download with internet download manager coral_raha 0 2,882 Jul-18-2021, 03:11 PM
Last Post: coral_raha
  Problem with NTLM to download data azahar 1 4,045 Dec-21-2016, 09:54 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020