繁体   English   中英

请求自动登录的Python Web抓取不起作用

[英]Python Web scraping with Requests Automated Login Not Working

我一直在尝试使用python requests模块通过网络抓取网站,并且需要登录该网站以检索所需的数据。 我到处环顾四周,但找不到原因。 到目前为止,这是我的代码:

import requests
import bs4 as bs

login_url = "__withheld__"
target_url = "__withheld__"

login_data = { "username": "my_username", "password": "my_password"}

with requests.Session() as s:
    page = s.get(login_url)
    page_login = s.post(login_url, data = login_data)
    page = s.get(target_url)
    final_page = bs.BeautifulSoup(page.content, 'lxml')
    print(final_page.title)

这是密码框的html:

<input name="username" type="text" id="username" class="metro-input" placeholder="Username" value="">
<span id="username-error" class=""></span>
<label class="ie789Only"> Password</label>
<input name="password" type="password" id="password" class="metro-input" placeholder="Password">
<input type="submit" name="button1" value="Sign in" id="button1" class="metro-button">

我认为这可能与要求用户单击按钮的网站有关,尽管我找不到任何解决方案。 当我登录自己时,我还尝试在开发人员控制台中查找任何张贴表格,但没有找到明确的表格来概述密码/用户名。 任何帮助表示赞赏。

更新如果有帮助,以下是指向具有相同安全功能的同一公司(隐私)运营的网站的链接: https : //ashwood-vic.compass.education/login.aspx?sessionstate=disabled

您可以一次尝试以下代码吗

import requests
import bs4 as bs
username = 'username of the site'
password = 'password of the site'

req = requests.get(login_url, auth=(username, password))
final_page = bs.BeautifulSoup(req.content, 'lxml')
print(final_page.title)

-请参阅此http://docs.python-requests.org/en/master/user/authentication/#basic-authentication

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM