问题描述
我将废弃公司的网站。我通常使用VPN访问它,因此不必每次都登录自己(我什至无法注销!)。为此,我一直在使用Requests库,但是在身份验证方面遇到了问题。
我尝试在Chrome的网络面板中提取cURL命令:
(如果您查看面板,可以看到Login.do。我认为该网站可能具有针对通过VPN连接的用户的真实登录信息)
然后,我用红色下划线转换了第一个GET的cURL,然后编写了此函数:
import requests
def enterWebsite():
cookies = {
'JSESSIONID': 'VscZfvnTbt1YnXFyQ5g245p59dWMWjM2lQX7QGGQrxn7QgwlYMTJ^!1826994054',}
headers = {
'Connection': 'keep-alive','Pragma': 'no-cache','Cache-Control': 'no-cache','Upgrade-Insecure-Requests': '1','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/86.0.4240.111 Safari/537.36','Accept': 'image/avif,image/webp,image/apng,image/*,*/*;q=0.8','Accept-Language': 'it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7','Origin': 'chrome://new-tab-page','Referer': 'http://mare.risorse.enel/',}
response = requests.get('http://mare.risorse.enel/',headers=headers,cookies=cookies)
return response.text
response = enterWebsite()
当我尝试运行它时,“响应”的状态代码始终为401:身份验证被拒绝。
我已经使用HTTPBasicAuth插入用户名和密码来尝试以下解决方案:
import requests
from requests.auth import HTTPBasicAuth
response = requests.get('http://mare.risorse.enel/',verify=False)
if response.status_code == 401:
response = requests.get('http://mare.risorse.enel/',auth=HTTPBasicAuth('user','pass'))
print (response)
但是没有,我无法弄清楚。
这很奇怪,因为当我在浏览器中搜索网站时,我已经登录了:我不需要“张贴”用户名和密码。所以我不明白为什么Python要求我在我连接到VPN时插入它们的原因...
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)