使用Python登录复杂的websi

2024-05-14 20:19:55 发布

您现在位置: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。然而,当我试图登录到这个使用上述代码,我仍然得到错误。你知道吗

我完全被难住了有什么想法吗?你知道吗

下面的最终代码

import requests
from bs4 import BeautifulSoup
import hashlib

username = 'username'
password = 'password'
login_url = 'https://inspire.flg360.co.uk/AuthUser.php'
login_info = {"strForwardURL": "",
              "strEmail": username,
              "intRememberMe": 1,
              "strResponse": ""}

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

# Start session.
session = requests.session()

# Get strResponse
strc = session.get(url=login_url)
strc = BeautifulSoup(strc.content, 'html.parser').findAll(attrs={"name": "strChallenge"})[0]['value']
strc_joined = strc + hashlib.md5(password.encode("utf-8")).hexdigest()
strresponse = hashlib.md5(strc_joined.encode("utf-8")).hexdigest()
login_info['strResponse'] = strresponse

#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)

Tags: httpsimportinfourlsessionusernameloginpassword
1条回答
网友
1楼 · 发布于 2024-05-14 20:19:55

看起来页面在https://inspire.flg360.co.uk/SignIn.php发送的实际POST请求还需要一些元素。也就是说,POST数据实际上看起来像:

strForwardURL=&strEmail=abc%40123.com&intRememberMe=1&strResponse=fdb4c46c5d0eeab6133be193afc7897e

字段是strForwardURLstrEmailintRememberMestrResponse。查看页面上的其余代码,当您单击submit按钮时,它会触发页面上的javascript:

    function fncSignIn() {

        var loginForm = document.getElementById("signinForm");

        if (loginForm.strEmail.value == "") {

            alert("Please enter your email address.");
            return false;

        }

        if (loginForm.strPassword.value == "") {

            alert("Please enter your password.");
            return false;

        }

        var submitForm = document.getElementById("submitForm");

        submitForm.strEmail.value = loginForm.strEmail.value;
        if (loginForm.intRememberMe.checked) submitForm.intRememberMe.value = 1;
        submitForm.strResponse.value = hex_md5(loginForm.strChallenge.value+hex_md5(loginForm.strPassword.value));

        submitForm.submit();

    }

在页面的其他地方,您可以在这里找到strChallenge字符串:

<input type="hidden" name="strChallenge" value="1d989603e448a1a0559f08bdc83a15522fbc6c0404ca66acc4cdd7aafe4039359e2fb23b706d60a3">

(顺便说一下,这个值在重新加载时会改变)

本质上,它不是字符串形式的密码,而是请求strChallenge字符串的md5hex摘要与密码的md5hex摘要连接。你知道吗

在python中,应该是这样的:

import hashlib
password = "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"}您应该可以登录。每次你想要抓取一个需要这个特殊登录的页面时,你应该能够简单地用BeautifulSoup4抓取strChallenge,计算出合适的strResponse,然后登录。你知道吗

相关问题 更多 >

    热门问题