Bottom Page

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
Larz60+ wrote Nov-11-2018, 10:32 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Quote
#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)
Age Doesn't Matter, Your Passion Does
- Shreya Jain
Quote
#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)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  o365 special subject mail download issue anna 3 228 May-16-2019, 07:16 PM
Last Post: micseydel
  How to get python to download YouTube videos in the background? Pythenx 11 757 Mar-25-2019, 04:57 AM
Last Post: samsonite
  Download multiple large json files at once halcynthis 0 257 Feb-14-2019, 08:41 AM
Last Post: halcynthis
  Import Library but Download first? OscarBoots 15 1,349 Feb-07-2019, 03:07 AM
Last Post: snippsat
  Download data from webpage after POST request AlDe 0 236 Feb-02-2019, 06:26 AM
Last Post: AlDe
  How can read and download the attachment from the mail using while loop in IMAPlib Py Samjith 0 506 Oct-11-2018, 07:15 AM
Last Post: Samjith
  download file from google drive .. evilcode1 7 1,691 Sep-21-2018, 06:13 PM
Last Post: evilcode1
  Download multiple images with one click andie31 0 483 Sep-13-2018, 10:37 AM
Last Post: andie31
  Download multiple images and rename them andie31 4 688 Sep-13-2018, 10:26 AM
Last Post: andie31
  download python 3.7.0 mainbane 1 503 Aug-09-2018, 06:51 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)