How can I register this particular site using Python queries?

I am trying to login to this site using the following query, but it does not work

The cookie never contains 'userid'.

What should i change? Do I need to add headers to my mail request?

import requests

payload = {
    'ctl00$MasterMainContent$LoginCtrl$Username': 'myemail@email.com',
    'ctl00$MasterMainContent$LoginCtrl$Password': 'mypassword',
    'ctl00$MasterMainContent$LoginCtrl$cbxRememberMe' : 'on',
}

with requests.Session() as s:
    login_page = s.get('http://www.bentekenergy.com/')
    response = s.post('http://benport.bentekenergy.com/Login.aspx', data=payload)
    if 'userid' in response.cookies:
       print("connected")
    else:
       print("not connected")

Edit 1 (the following comments): I'm not sure what to add to the request headers, below is what I tried, but to no avail.

request_headers = {
    'Accept':'image/webp,image/*,*/*;q=0.8',
    'Accept-Encoding':'gzip, deflate, sdch, br',
    'Accept-Language':'en-US,en;q=0.8',
    'Connection':'keep-alive',
    'Cookie':'ACOOKIE=C8ctADJmMTc1YTRhLTBiMTEtNGViOC1iZjE0LTM5NTNkZDVmMDc1YwAAAAABAAAASGYBALlflFnvWZRZAQAAAABLAAC5X5RZ71mUWQAAAAA-',
    'Host':'statse.webtrendslive.com',
    'Referer':'https://benport.bentekenergy.com/Login.aspx',
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
}

Edit 2 (following stovfl's answer):

Now I use the following payload, fill all the attributes with the value in the form, and fill it with the username, password and memMe. I also tried with the following headers in the request. Still not connected

payload = {
    '__VIEWSTATE' : '',
    '__VIEWSTATEGENERATOR' : '',
    '__PREVIOUSPAGE' : '',
    '__EVENTVALIDATION' : '',
    'isAuthenticated' : 'False',
    'ctl00$hfAccessKey' : '',
    'ctl00$hfVisibility' : '',
    'ctl00$hfDateTime' : '',
    'ctl00$hfHash' : '',
    'ctl00$hfAnnouncementsUrl' : '',
    'ctl00$MasterMainContent$LoginCtrl$Username' : '',
    'ctl00$MasterMainContent$LoginCtrl$Password' : '',
    'ctl00$MasterMainContent$LoginCtrl$cbxRememberMe' : '',
}

request_headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding':'gzip, deflate, br',
        'Accept-Language':'en-US,en;q=0.8',
        'Cache-Control':'max-age=0',
        'Connection':'keep-alive',
        'Content-Length':'7522',
        'Content-Type':'application/x-www-form-urlencoded',
        'Cookie':'',
        'Host':'benport.bentekenergy.com',
        'Origin':'https://benport.bentekenergy.com',
        'Referer':'https://benport.bentekenergy.com/Login.aspx',
        'Upgrade-Insecure-Requests':'1',
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
}

with requests.Session() as s:
response = s.get('http://benport.bentekenergy.com/Login.aspx')
soup = BeautifulSoup(response.text, "html.parser")
if soup.find("input", {"name" : "ctl00$MasterMainContent$LoginCtrl$Username"}):
    print("not connected")
    soup = BeautifulSoup(response.text, "lxml")
    for element in soup.select("input"): 
       if element.get("name") in payload:
           payload[element.get("name")] = element.get("value")

    payload['ctl00$MasterMainContent$LoginCtrl$Username'] = 'myemail@email.com'
    payload['ctl00$MasterMainContent$LoginCtrl$Password'] = 'mypassword'
    payload['ctl00$MasterMainContent$LoginCtrl$cbxRememberMe'] = 'on'

    response = s.post('http://benport.bentekenergy.com/Login.aspx', data=payload, headers=request_headers)

    print (s.cookies)
    soup = BeautifulSoup(response.text, "html.parser")
    if soup.find("input", {"name" : "ctl00$MasterMainContent$LoginCtrl$Username"}):
            print("not connected")
    else:
            print("connected")

s.cookies contains:

<RequestsCookieJar[<Cookie BenportState=q1k2r2eqftltjm55igy5mg55 for .bentekenergy.com/>, <Cookie RememberMe=True for .bentekenergy.com/>]>

Change 3 (answer!):

I added

'__EVENTTARGET' : ''

in the payload and filled it with the value 'ctl00 $ MasterMainContent $ LoginCtrl $ btnSignIn'

! NB: ,

+4
1

:... , '__EVENTTARGET', . 'ctl00 $MasterMainContent $LoginCtrl $btnSignIn'. !

, Submit Button, Javascript:

href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$headerLoginCtrl$btnSignIn&quot;,

: SO POST


:... (Edit 2).

  • http https
    https.

  • <RequestsCookieJar , .
    : if soup.find("input", {"name"....
    ?
    ?

  • BeautifulSoup(..., Session s Cookie.
    . response = s.get('<url to some resticted page>

  • request_headers 'User-Agent'


<form> :
URL- : https://benport.bentekenergy.com/Login.aspx
: : /Login.aspx, : post

value empty : Pre-Set-Values ​​ .

 1:input type:hidden   value:/wEPDwUKLT... id:__VIEWSTATE 
 2:input type:hidden   value:0BA31D5D      id:__VIEWSTATEGENERATOR 
 3:input type:hidden   value:2gILTn0H1S... id:__PREVIOUSPAGE 
 4:input type:hidden   value:/wEWDAKIr6... id:__EVENTVALIDATION 
 5:input type:hidden   value:False         id:isAuthenticated 
 6:input type:hidden   value:nu66O9eqvE    id:ctl00_hfAccessKey 
 7:input type:hidden   value:public        id:ctl00_hfVisibility 
 8:input type:hidden   value:08%2F16%2F... id:ctl00_hfDateTime 
 9:input type:hidden   value:3AB353573D... id:ctl00_hfHash 
10:input type:hidden   value://announce... id:ctl00_hfAnnouncementsUrl 
11:input type:text     value:empty         id:ctl00_MasterMainContent_LoginCtrl_Username 
12:input type:password value:empty         id:ctl00_MasterMainContent_LoginCtrl_Password 
13:input type:checkbox value:empty         id:ctl00_MasterMainContent_LoginCtrl_cbxRememberMe 
+1

Source: https://habr.com/ru/post/1683833/


All Articles