[英]Web scraping with python, request.json() shows status_code of 200 but can not extract json data
[英]Python web scraping with requests - status code 200, but no successful login
准备问我的第一个问题和编程新手,所以请耐心等待:我正在尝试使用 Python 和requests
-library 进行一些有趣的网络抓取。 但是,我无法成功登录该网站。 这是我尝试登录的网站上表单的 html 代码:
<body class="login"><div class="wrapper">
<div class="wrapper_forms">
<!-- Login Form -->
<form class="login" action="?" method="post">
<fieldset>
<legend>Login</legend>
<label for="login_username">Benutzername:</label>
<input id="login_username" type="text" name="username" autofocus="autofocus" /><em>*</em><br>
<label for="login_password">Passwort:</label>
<input id="login_password" type="password" name="password" /><em>*</em><br>
<em>* required</em><br>
<input type="hidden" name="form" value="login" />
<input id="screen_width" type="hidden" name="screen_width" value="" />
<input type="submit" value="Login" />
<br><a class="pw_reset" href="?action=pw_reset">Passwort vergessen</a>
<br>
</fieldset>
</form>
这是我的代码的缩写版本。 我还对我为了可读性而排除的 get 和 post 请求的响应对象进行了一些额外的检查。 该站点当前没有有效的 SSL 证书,因此verify = False
参数。
from bs4 import BeautifulSoup
import requests
# Start session
session = requests.Session()
# payload
payload = {'username':'****',
'password':'****',
'form':'login',
'screen_width':'1920'}
# variables
login = "https://altklausurendb.de/login.php"
dest = "https://altklausurendb.de/index.php"
# initial get request to retrieve cookies and headers
s = session.get(login, verify = False)
cookies = s.cookies.get_dict()
headers = s.headers
# distribute payload
r1 = session.post(login, data = payload, cookies = cookies, headers = headers, verify = False)
#download content of destination site
r2 = session.get(dest, cookies = cookies, verify = False)
soup = BeautifulSoup(r2.text, 'html.parser')
print(soup)
打印函数的输出给了我上面的 html 表单,而不是https://altklausurendb.de/index.php
的内容,后者是第二个GET
请求的dest
变量。
在阅读了这里、reddit 和 WWW 的其他内容后,我自己尝试过的事情:
POST
请求中显式传递 cookie 和标头即可运行它(显而易见)POST
请求,使用网络点击下的 Chrome 开发工具。 甚至在脚本的单独版本中手动复制标头,以防requests
header
函数以某种方式提供错误的标头。GET
和POST
请求创建的response
运行所有检查,有趣的是,无论我在字典中提供什么用户名和密码,状态代码始终为200
--> 这让我怀疑,如果网站没有以403
响应错误凭据,则POST
请求未正确提交。 我看到某些网站在<input type="submit">
语句中也有一个name
-variable,您必须将其传递给字典。 然而,这个网站似乎没有它,基本上这就是我现在卡住的地方。
如果他们想自己重现脚本的行为,我愿意将我在网站上创建的测试帐户的凭据发送给某人。
感谢您的耐心等待,我知道网络抓取问题并不是最喜欢的问题,我感谢您的帮助!
扔掉所有这些,得到硒模块。 BS4 适合抓取 HTML,但如果您要抓取动态页面,则需要 selenium。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.