问题描述
我正在尝试使用请求下载一些文档,但该页面将我重定向到用户登录屏幕并因此下载 HTML 页面.
I am trying to download some documents using requests, but the page is redirecting me to a userlog in screen and therefor downloading the HTML page.
我尝试过:
c=requests.get(url,auth=HTTPBasicAuth('user','pass'))
但我没有通过身份验证.
But I'm not getting authenticated.
我也试过 vanilla 和 Digest.
I've also tried vanilla and Digest.
表单本身看起来像这样:
The form itself looks like this:
<input id="username" name="username" class="required" tabindex="1" type="text" value="" size="25" autocomplete="false"/>
<br/>
<label for="password">Password</label>
<input id="password" name="password" class="required" tabindex="2" type="password" value="" size="25" autocomplete="off"/>
我是否需要将用户名和密码作为有效负载的一部分传递?如果是这样,我该怎么做?到目前为止,我已经尝试了几种不同的方法.
Do I need to pass in the username and password as a part of the payload? If so, how do I do that? I've tried a few different ways so far.
推荐答案
基本上,它与从页面中获取身份验证 ID 并传入 cookie 相关.
Basically, it had to do with grabbing the authentication ID off the page and passing in cookies.
这基本上就是我所做的:
This is basically what I did:
from bs4 import BeautifulSoup as bs
import requests
s = requests.session()
url = r'url_i_care_about'
def authenticate(s, url):
headers = {'username': 'myuser', 'password': 'mypasss', '_Id': 'submit'}
page=s.get(url)
soup=bs(page.content)
value=soup.form.find_all('input')[2]['value']
headers.update({'value_name':value})
auth = s.post(url, params=headers, cookies=page.cookies)
这篇关于身份验证和 python 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!