简体   繁体   中英

I'm trying to get an excel sheet downloaded using python requests module and getting junk output

I'm trying to download an excel file which is uploaded on a Sharepoint 2013 site.

My code is as follows:

import requests
url='https://<sharepoint_site>/<document_name>.xlsx?Web=0'
author = HttpNtlmAuth('<username>','<passsword>')
response=requests.get(url,auth=author,verify=False)
print(response.status_code)
print(response.content)

This gives me a long output which is something like:

x00docProps/core.xmlPK\\x01\\x02-\\x00\\x14\\x00\\x06\\x00\\x08\\x00\\x00\\x00!\\x00\\x7f\\x8bC\\xc3\\xc1\\x00\\x00\\x00"\\x01\\x00\\x00\\x13\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xb8\\xb9\\x01\\x00customXml/item1.xmlPK\\x05\\x06\\x00\\x00\\x00\\x00\\x1a\\x00\\x1a\\x00\\x12\\x07\\x00\\x00\\xd2\\xba\\x01\\x00\\x00\\x00'

I did something like this before for another site and I got xml as output which was acceptable for me but I'm not sure how to handle this data.

Any ideas to process this to be like xlsx or xml?

Or maybe to download the xlsx another way?(I tried doing it through the wget library and the excel seems to get corrupted)

Any ideas would be really helpful.

Regards, Karan

Its too late but i got similar issue... thought it might help someone else.

try writing the output to a file or apply some encoding while printing.

writing to a file:

file=open("./temp.xls", 'wb')
file.write(response.content)
file.close()

or

file=open("./temp.xls", 'wb')
file.write(response.text)
file.close()

printing with encoding

print ( resp.text.encode("utf-8") )

or

print ( resp.content.encode("utf-8") )

!Make appropriate imports. !try 'w' or 'wb' for file write.

Hope this helps.

It seems that the file is encrypted and request can't handle this.
Maybe the web service provides an API for downloading and secure decoding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM