0

I am currently working on a project and I have been stuck in exporting a table from the servicenow website using the code below, but is returning a URL redirection instead of the CSV.

Code:

url = "https://url.service-now.com/nav_to.do?" \
                    "uri=server_list.do?sysparm_query=active=false%26CSV"


response = requests.get(url, stream=True, headers=headers, allow_redirects=True, verify="C:\\Users\user\PycharmProjects\cert.crt")

LocalFilePath = "C:\\Users\user\Documents\Projects\Reporting\server.csv"


with open(LocalFilePath, 'wb') as csv2:
    csv2.write(response.content)

Any help and suggestion is much appreciated.

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
LSB
  • 1

1 Answers1

0

The first thing I notice is that your path information is not quite correct.

Wrong

verify="C:\\Users\user\PycharmProjects\cert.crt"
LocalFilePath = "C:\\Users\user\Documents\Projects\Reporting\server.csv"

It could be that \R is interpreted as a "carriage return" (part of a line break): https://learn.microsoft.com/en-us/cpp/c-language/escape-sequences (I don't have Windows here right now.)

Corrected 1

verify="C:\\Users\\user\\PycharmProjects\\cert.crt"
LocalFilePath = "C:\\Users\\user\\Documents\\Projects\\Reporting\\server.csv"

Corrected 2 (recommended)

verify=r"C:\Users\user\PycharmProjects\cert.crt"
LocalFilePath = r"C:\Users\user\Documents\Projects\Reporting\server.csv"

Furthermore, you could try setting allow_redirects to False and output response.is_redirect or response.is_permanent_redirect.

qräbnö
  • 2,722
  • 27
  • 40
  • Hi qräbnö, thank you for the help. That works perfectly. – LSB Jul 23 '18 at 22:34
  • Hi qräbnö, just a follow up on this. After about 5 times of executing the code, I am now getting a 302 http response and the csv file is empty. I have not made any other changes in the code other than modifying the allow_redirection to false. Just to add also, I am really a newbie in python – LSB Jul 24 '18 at 00:32
  • OK, set allow_redirects back to True. I just wanted to see if you get anything displayed at response.is_redirect (for debugging). If I could help you in any way, I would be happy about an upvote of my answer. ;) – qräbnö Jul 24 '18 at 08:31
  • Hi qräbnö, may i ask your suggestion on this? – LSB Jul 24 '18 at 09:58
  • Maybe use `import time; time.sleep(2) # seconds` between requests? Then you might not get blocked so quickly. If you call the same URL with your browser, does everything work? – qräbnö Jul 24 '18 at 10:23
  • Yes, that is correct. If i browse the URL directly it is working perfectly fine. – LSB Jul 24 '18 at 21:12
  • You can probably use Selenium to download the CSV, but that might be a little too much of a good thing: https://stackoverflow.com/questions/18439851/how-can-i-download-a-file-on-a-click-event-using-selenium – qräbnö Jul 24 '18 at 21:16
  • (Don't use PhantomJS or Firefox for Selenium. Use Chrome and chromedriver.) – qräbnö Jul 24 '18 at 21:22