[英]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. 我正在尝试下载一个Excel文件,该文件已在Sharepoint 2013网站上上传。
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'
x00docProps / core.xmlPK \\ x01 \\ x02- \\ x00 \\ x14 \\ x00 \\ x06 \\ x00 \\ x08 \\ x00 \\ x00 \\ x00!\\ x00 \\ x7f \\ x8bC \\ xc3 \\ xc1 \\ x00 \\ 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 \\ 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. 我之前在另一个站点上做了类似的事情,但我得到了xml作为输出,这对我来说是可以接受的,但是我不确定如何处理该数据。
Any ideas to process this to be like xlsx or xml? 有什么想法可以像xlsx或xml这样处理吗?
Or maybe to download the xlsx another way?(I tried doing it through the wget library and the excel seems to get corrupted) 或者也许以另一种方式下载xlsx?(我尝试通过wget库执行此操作,而Excel似乎已损坏)
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.
!尝试'w'或'wb'进行文件写入。
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. 也许Web服务提供了用于下载和安全解码的API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.