微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决401未经授权的错误,带有Requests.get

我正在尝试从公司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网站本身阻止了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。