我正在尝试使用requests来抓取数据登录https://www.voxbeam.com/login。我是Python初学者,主要只是按照教程做了一些练习,还自己用BeautifulSoup进行了一些网页数据抓取。
看着HTML代码:
<form id="loginForm" action="https://www.voxbeam.com//login" method="post" autocomplete="off">
<input name="userName" id="userName" class="text auto_focus" placeholder="Username" autocomplete="off" type="text">
<input name="password" id="password" class="password" placeholder="Password" autocomplete="off" type="password">
<input id="challenge" name="challenge" value="78ed64f09c5bcf53ead08d967482bfac" type="hidden">
<input id="hash" name="hash" type="hidden">
我知道应该使用方法post,并发送userName和password。
我正在尝试这个:
import requests
import webbrowser
url = "https://www.voxbeam.com/login"
login = {'userName': 'xxxxxxxxx',
'password': 'yyyyyyyyy'}
print("Original URL:", url)
r = requests.post(url, data=login)
print("\nNew URL", r.url)
print("Status Code:", r.status_code)
print("History:", r.history)
print("\nRedirection:")
for i in r.history:
print(i.status_code, i.url)
# Open r in the browser to check if I logged in
new = 2 # open in a new tab, if possible
webbrowser.open(r.url, new=new)
成功登录后,我期望能够进入r仪表板的URL,以便开始抓取所需的数据。
当我以身份验证信息替换xxxxxx和yyyyyy运行代码时,我会得到以下输出:
Original URL: https://www.voxbeam.com/login
New URL https://www.voxbeam.com/login
Status Code: 200
History: []
Redirection:
Process finished with exit code 0
我在浏览器中打开了一个新选项卡,网址是www.voxbeam.com/login。
代码有问题吗?HTML中漏掉了什么吗?期望从r中获取仪表板URL并重定向尝试在浏览器选项卡中打开URL以直观检查响应,还是我应该用不同的方法来做这件事情?
我已经阅读了这里几天的很多类似的问题,但似乎每个网站的身份验证过程都略有不同,而我也查看了http://docs.python-requests.org/en/latest/user/authentication/,其中描述了其他方法,但我没有在HTML中找到任何提示表明我应该使用那些方法之一而不是post。
我也尝试过。
r = requests.get(url, auth=('xxxxxxxx', 'yyyyyyyy'))
但它似乎也不起作用。