401未经授权的错误,带有Requests.get()

时间:2020-10-02 18:55:35

标签: python function api sharepoint python-requests-html

我正在尝试从公司Share Point网站下载文件,但此401错误使我受阻。以下是我正在使用的代码,我尝试使用请求模块固有的基本身份验证,但它不会更改结果。我不确定该网站使用哪种加密方式,但是如果有人可以提供任何指导或帮助,我以及将不得不一次手动下载500多个文件的人将不胜感激。

import requests
import pandas as pd
from requests.auth import HTTPBasicAuth

url = 'http://SharePoint_Site_address.aspx'
auth = HTTPBasicAuth('username', 'password')
r = requests.get(url=url, auth=auth)

    def download_file(url):

        # this will grab the filename from the url
        filename = url.split('/')[-1]

        r

        with open(filename, 'wb') as output_file:
           output_file.write(r.content)

        print('ok')
        print('status: ', r.status_code)

# The purpose of this block is to create a list of the files I need to iterate through with the the loop
pd.set_option('display.max_colwidth', None)
df = pd.read_excel(r'O:\Procurement Planning\QA\VSAF_test.xlsx')
df['Name'] = 'https://SharePoint_Site_Path/' + df['Name'].astype(str)
file = df['Name']
file = file.head()

for url in file:
    print(url)
    download_file(url)

打印的URL手动下载文件,因此,如果单击它,它将正确打开。但是,通过循环本地下载的文件将不会打开,并且Excel表示格式或扩展名无效。另外,我可以确认该函数没有返回任何实际值,但这似乎无关紧要,因为我被Share Point网站本身阻止了。

0 个答案:

没有答案