使用 Python 登录一个棘手的网站

我作为数据分析师从事数字营销工作。我的部门使用第三方来帮助吸引更多客户。这些第三方中的每一个都有一个网站,用于显示他们为我们公司带来了多少客户。我的部分工作是从每个网站收集数字并将它们放入报告中,这是一个漫长的手动过程。到目前为止,我已经成功登录了我们的一些第三方网站并提取了一些数据。但是,有一个网站我在登录时遇到了一些问题... https://inspire.flg360.co.uk/SignIn.php。我还需要将会话重定向到另一个 URL 以从中抓取数据。


我编写了一些代码,可以成功登录到我需要从中获取信息的其他网站。


import requests

from bs4 import BeautifulSoup

import re


username = 'username'

password = 'password'

scrape_url = 'https://portal.mvfglobal.com/index.php/dashboard'


login_url = 'https://portal.mvfglobal.com/index.php/login/login'

login_info = {'login_name': username, 'login_pass': password}


#Start session.

session = requests.session()


#Login using your authentication information.

session.post(url=login_url, data=login_info)


#Request page you want to scrape.

url = session.get(url=scrape_url)


soup = BeautifulSoup(url.content, 'html.parser')


print(soup)

但是,当我尝试使用相同的方法登录https://inspire.flg360.co.uk/SignIn.php 时,我遇到了一些问题。


import requests

from bs4 import BeautifulSoup


username = 'username'

password = 'password'

login_url = 'https://inspire.flg360.co.uk/SignIn.php'

login_info = {'strEmail': username, 'strPassword': password}


scrape_url = 'https://inspire.flg360.co.uk/AuthUser.php'


#Start session.

session = requests.session()

#Login using your authentication information.

session.post(url=login_url, data=login_info)

#Request page you want to scrape.

url = session.get(url=scrape_url)


soup = BeautifulSoup(url.content, 'html.parser')


print(soup)

当我检查页面元素时,我注意到 302 响应重定向到https://inspire.flg360.co.uk/AuthUser.php。但是,当我尝试使用上面的代码登录时,我仍然遇到错误。


我完全难倒任何想法?


繁星点点滴滴
浏览 275回答 1
1回答

SMILET

看起来https://inspire.flg360.co.uk/SignIn.php页面发送的实际 POST 请求 还有一些需要的元素。也就是说,POST 数据实际上看起来像:strForwardURL=&strEmail=abc%40123.com&intRememberMe=1&strResponse=fdb4c46c5d0eeab6133be193afc7897e这些字段是strForwardURL,strEmail,intRememberMe,和strResponse。查看页面上的其余代码,当您单击提交按钮时,它会触发页面上的这段 javascript:&nbsp; &nbsp; function fncSignIn() {&nbsp; &nbsp; &nbsp; &nbsp; var loginForm = document.getElementById("signinForm");&nbsp; &nbsp; &nbsp; &nbsp; if (loginForm.strEmail.value == "") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Please enter your email address.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (loginForm.strPassword.value == "") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Please enter your password.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var submitForm = document.getElementById("submitForm");&nbsp; &nbsp; &nbsp; &nbsp; submitForm.strEmail.value = loginForm.strEmail.value;&nbsp; &nbsp; &nbsp; &nbsp; if (loginForm.intRememberMe.checked) submitForm.intRememberMe.value = 1;&nbsp; &nbsp; &nbsp; &nbsp; submitForm.strResponse.value = hex_md5(loginForm.strChallenge.value+hex_md5(loginForm.strPassword.value));&nbsp; &nbsp; &nbsp; &nbsp; submitForm.submit();&nbsp; &nbsp; }在页面的其他地方,您可以在strChallenge此处找到字符串:<input type="hidden" name="strChallenge" value="1d989603e448a1a0559f08bdc83a15522fbc6c0404ca66acc4cdd7aafe4039359e2fb23b706d60a3">(顺便说一下,这个值在重新加载时会发生变化)本质上,它要求strChallenge字符串的 md5 十六进制摘要与密码的 md5 十六进制摘要连接,而不是字符串形式的密码。在python中,它会是这样的:import hashlibpassword = "abcdefg12345"strc = "1d989603e448a1a0559f08bdc83a15522fbc6c0404ca66acc4cdd7aafe4039359e2fb23b706d60a3"strc_joined = strc + hashlib.md5(password.encode("utf-8")).hexdigest()strresponse = hashlib.md5(strc_joined.encode("utf-8")).hexdigest()print(strresponse)本例中的输出为 0d289f39067a25430d4818fe38046372将原始请求中的 postdata 变为:{"strForwardURL":"", "strEmail":"abc@123.com", "intRememberMe": 1, "strResponse": "0d289f39067a25430d4818fe38046372"}并且您应该能够登录。每次您想抓取需要此特定登录的页面时,您应该能够简单地strChallenge使用 BeautifulSoup4抓取,计算正确的strResponse,然后登录。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python