我可以使用集成的 windows 身份验证和 BeautifulSoup 吗?

can I use integrated windows authentication with BeautifulSoup?

我正在尝试使用 BeautifulSoup 抓取内部网页。该网页本身只对我们网络上的某些用户开放,并使用集成的 Windows 身份验证。我在下面有一个简单的脚本可以正常工作,但我必须提供凭据。

import requests
from requests_ntlm import HttpNtlmAuth
from bs4 import BeautifulSoup

url = "internal page"
content = requests.get(url, auth=HttpNtlmAuth('domain\myusername', 'mypw'))

result = BeautifulSoup(content.text, 'html.parser')

print(result)

我是否可以 运行 使用集成的 Windows 身份验证,从而无需提供我的凭据?

谢谢。

requests-ntlm 回购中有一个已关闭的 issue 问题:

Is there a way to authenticate with currently logged user's credentials instead of providing login/password directly inside the script?

以及库作者的回复:

Not as far as I know....


也看看这个question。有一些替代方法来存储您的秘密。

我个人更喜欢环境变量之类的 secrets.py:

import os

user = os.getenv('MY_USER')
password = os.getenv('MY_PASSWORD')

然后您可以从那里导入您的秘密:

from secrets import user, password

另一个不太轻量级的选项是使用 playwrihtselenium 打开真正的浏览器,这可能能够在没有硬编码登录名和密码的情况下加载页面