Login to Python Session

Asked 2 years ago, Updated 2 years ago, 61 views

I'm studying Python with a book called "Making a Web Crawler with Python."

Read the session login part, log in to Africa TV using the session, and then use your item page

I'm trying to bring it, but it doesn't work. I don't know what the problem is, so I'm looking for help

session = requests.Session()
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'} 

param = {
    "szWork": 'login',
    "szType" : 'json',
    "szUid" : "#ID",
    "szPassword": '#Password',
    "isSaveId" : 'false',
    "szScriptVar": 'oLoginRet',
    "szAction" : "",
    "isLoginRetain" : 'N'
}
logUrl ='https://login.afreecatv.com/afreeca/login.php'
myitemUrl = 'https://point.afreecatv.com/report/AfreecaUseList.asp'

s = session.post(logUrl, param)
print("cookie is set to  : ")
print(s.cookies.get_dict())
print("----------------")
#print("Go to My Item Page") # I don't think I can log in, so I'll comment on the 3 lines below
#s = session.get(myitemUrl)
#print(s.text)

If you run the code above, you'll get the result as below

cookie is set to  :
{'AbroadChk': 'FAIL', 'AbroadVod': 'FAIL'}
---------------

In the example of the book, there was only an ID password

When I logged in to Africa on f12 and looked at the form data, it looked like a picture

I made the param shape, but did I make the param wrong?

python session login

2022-09-21 18:37

1 Answers

It's not that I made it wrong.It would have been done by the time the book was written, but Africa TV changed compared to that time.

Change to logUrl='https://login.afreecatv.com/afreeca/login.php' instead of logUrl='https://login.afreecatv.com/app/LoginAction.php'.

The login method returns the result to json and redirects it.

If you look at the JavaScript function below, only 1 is a success and the rest is a failure.

function onAfterSecondLogin(data, isApp)
{
    var szOnAfterLoginMsg ="";

    nRet = Number(data.RESULT);

    switch(nRet)
    {
        case 1:
            var szRequestUri = (szAfterLoginPage.indexOf("hidden_app") > 0) ? "" : GetCookie("request_uri");

            if(szAfterLoginPage =="" && szRequestUri) 
            {
                szAfterLoginPage = szRequestUri;
            }

            if( szRequestUri )
            {
                var expireDate = newDate(); // Create a new expireDate object and
                expireDate.setDate(expiryDate.getDate()-1); // Set the date value of the expiration date one day less than today (yesterday);
                document.cookie = "request_uri=; path=/; domain=afreecatv.com;expires=" + expireDate.toGMTString()+";";
            }
            isLogin =true;

            if(data && data.notChangePwd && isApp == false)
            {
                var url = "https://member.afreecatv.com/app/campaign_pw.php"
                    ,request_uri = getURLParameter(szAfterLoginPage, "request_uri")
                    ,isFromPop = 0 > location.href.indexOf('szFrom=pop') ? false : true
                    ;
                if( isApp )
                {
                    szAfterLoginPage += '&isChangePwd=1';
                }
                else // Do not pop-up the installation player password change window (handled by itself)
                {
                    if( !isFromPop )
                    {
                        url += "?request_uri=" + (request_uri ? request_uri : "http://www.afreecatv.com");
                    }
                    if(parent != window)
                    {
                        if( typeof(data.item) != 'undefined' && typeof(data.comment) != 'undefined' )
                        {
                            alert( data.comment );
                        }
                        parent.location.href= url;
                        return;
                    }
                    else
                    {
                        if(location.href.indexOf("szFrom=full") >= 0)
                        {
                            if( typeof(data.item) != 'undefined' && typeof(data.comment) != 'undefined' )
                            {
                                alert( data.comment );
                            }
                            location.href= url;
                            return;
                        }
                        else window.open(url);
                    }
                }
            }
            try
            {
                if( typeof(parent.layerPopup) == 'function' && typeof(data.item) != 'undefined' )
                {
                    parent.layerPopup( data.item );
                }
                else if( typeof(data.item) != 'undefined' && typeof(data.comment) != 'undefined' )
                {
                    alert( data.comment );
                }
            }
            catch(e)
            {
            }

            onAfterLoginLogOut();
            break;
        case 0:
            szOnAfterLoginMsg ="You have entered an incorrect password.";
            alert(szOnAfterLoginMsg);
            break;
        case -1:
            szOnAfterLoginMsg ="You have entered an incorrect password.";
            alert(szOnAfterLoginMsg);
            break;
        case -4:
            szOnAfterLoginMsg = "The service has been suspended due to a violation of African TV operating principles.\nIf you commit the same violation by generating false IDs through theft of resident registration numbers during the suspension period, you may be subject to legal punishment under the relevant laws apart from sanctions.";
            alert(szOnAfterLoginMsg);
            break;
        default:
            szOnAfterLoginMsg = "Login failed.("+nRet+")";
            alert(szOnAfterLoginMsg);
            break;
    }

    //User function
    try{
        onLogin(nRet,szOnAfterLoginMsg); // Used by Chocolate Factory
    }catch(e){}
}
import requests

session = requests.Session()
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'} 

param = {
    "szWork": 'login',
    "szType" : 'json',
    "szUid" : '',
    "szPassword" : '',
    "szAction" : "",
    "isLoginRetain" : 'N'
}
logUrl ='https://login.afreecatv.com/app/LoginAction.php'
myitemUrl = 'https://point.afreecatv.com/report/AfreecaUseList.asp'

s = session.post(logUrl, data=param)
#print("cookie is set to  : ")
#print(s.cookies.get_dict())
print(s.text) # The corresponding {"RESULT":1} The RESULT value must be verified to be 1.
print("----------------")


s2 = session.post(myitemUrl)
print(s2.text)


2022-09-21 18:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.