简体   繁体   English

我正在尝试使用python请求模块下载Excel工作表并获取垃圾输出

[英]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.

相关问题 当我尝试通过 python 脚本执行 shell 命令时出现错误“找不到模块” - I'm getting an error “module not found ” when I'm trying to execute shell commands through a python script 我没有得到我想要使用 python 的确切输出 - I'm not getting the exact output i want using python 我正在尝试读取 JSON 数据,然后尝试使用 Python 将该数据填充到 excel 工作表中 - I am trying to read JSON Data then trying to populate that data into a excel sheet using Python 为什么在 Python 中尝试使用 requests.get 时出现 [403] 错误 - Why am I getting a [403] Error when trying to use requests.get in Python Python:使用请求未完全下载文件 - Python: File is not fully downloaded using requests 在使用 For 循环然后尝试按名称访问 Python 系列中的列后,我收到一条关键错误消息 - I'm getting a Key Error Message after using a For loop and then trying to access the columns by name in a Python Series 在python中使用input()获取用户输入后,我没有任何输出。 - I'm not getting any output after taking user input using input() in python. 我没有得到 else 输出 - I'm not getting the else output 如何在Python中将字典输出到Excel工作表 - How to output my dictionary to an Excel sheet in Python 我正在尝试提取span_id内的文本,但使用python beautifulsoup得到空白输出 - I am trying to extract text inside span_id, but getting blank output using python beautifulsoup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM