Python - Downloading file from web page

Viewed 66

I'm attempting to automate the download of a document from a web page. I have a url which, when pasted into the address bar, downloads the file just fine. When using Python requests, it downloads the source of the page, not the file as intended. My attempted code is below:

url = 'https://my.url.com/with?parameters=here'
r = requests.get(url,allow_redirects=True)
open('file.xlsx','wb').write(r.content)

The web page has a JS function (see below) tied to the download link 'onclick' and in the url there is a export_xlsx parameter that I believe is mapping to that function. There are no errors when the code is executed, it creates the 'file.xlsx' just fine, but it contains the page's html source instead of the correct content. Again, pasting the exact same url into a browser downloads the file properly.

JS Function:

function exportExcel() {
  document.search_form.xaction.value = "export_xlsx";
  document.search_form.search_criteria.value = "";
  document.search_form.submit();
  document.search_form.xaction.value = "";
  return false;
}

Any thoughts on what I'm missing?

Note, the link is internal to my company, otherwise I'd share so interested parties could test.

0 Answers
Related