Logging in using Python queries

I am trying to enter a website using Queries and it seems to click on the wall. Any advice would be appreciated.

I'm trying to log in to economist.com (for no reason, I just have a username and password) whose login page is in https://www.economist.com/user/loginand whose login form has an attribute action="https://www.economist.com/user/login?destination=%2F".

Using the Chrome Developer Tools, the login form data looks like this:

name: ///////// 
pass: ////////
form-build-id: form-483956e97a61f73fbc0ebf06b04dbe3f
form_id: user_login
securelogin_original_baseurl: https://www.economist.com
op: Log in

My code GETS a login page, uses BeautifulSoup to determine form_id; POST attempts to log in using my username and password, extracted form_id and other hidden variables; and then uses BeautifulSoup to check the home page to see if the banner has a login or logout link to determine if I really have logged in.

The code is as follows:

import requests
from bs4 import BeautifulSoup

# Setting user agent to a real browser instead of requests
headers = requests.utils.default_headers()
headers.update(
    {
        'User-Agent': 'Mozilla/5.0',
    }
)

# create a session and login
s = requests.Session()
login_page = s.get('https://www.economist.com/user/login', headers=headers)
login = BeautifulSoup(login_page.text, 'lxml')
form = login.select_one("form > div > input")
payload = {
            'name' : '////////////',
            'pass' : '////////',
            'form_build_id' : form['value'],
            'form_id' : 'user_login',
            'securelogin_original_baseurl' : 'https://www.economist.com',
            'op' : 'Log in'
            }
response = s.post("https://www.economist.com/user/login?destination=%2F",
data=payload, headers=headers)

# check homepage banner to see if login or logout link is there
url = "https://www.economist.com/"
r = s.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'lxml')
banner = soup.select("div > div > span > a")
for table_row in banner:
    print(table_row['href'])

When run, this code shows that the banner still has a login link instead of an exit link, which I suppose means that it is not logged in. I know that I must have made a very simple mistake here, but after reading other similar questions here, I can’t find where I am going. I would appreciate any advice on doing this work.

+4
1

, 1 .

form = login.select_one("form > div > input") 

To:

form = login.find('input', attrs={'name': "form_build_id"})

, , , , . http://www.economist.com/subscriptions/activation

, , https://www.economist.com/user/register?destination=subscriptions%2Factivation&rp=activating

import requests
from bs4 import BeautifulSoup

# Setting user agent to a real browser instead of requests
headers = requests.utils.default_headers()
headers.update(
    {
        'User-Agent': 'Mozilla/5.0',
    }
)

# create a session and login
s = requests.Session()
login_page = s.get('https://www.economist.com/user/login', headers=headers)
login = BeautifulSoup(login_page.text, 'lxml')
form = login.find('input', attrs={'name': "form_build_id"})#works

payload = {
            'name' : '*****',
            'pass' : '*****',
            'form_build_id' : form['value'],
            'form_id' : 'user_login',
            'securelogin_original_baseurl' : 'https://www.economist.com',
            'op' : 'Log in'
            }
response = s.post("https://www.economist.com/user/login?destination=%2F",
data=payload, headers=headers)

activation_page = s.get('http://www.economist.com/subscriptions/activation', headers=headers)
if activation_page.url == 'https://www.economist.com/user/register?destination=subscriptions%2Factivation&rp=activating':
    print"Failed to login"
elif activation_page.url == 'http://www.economist.com/subscriptions/activation':
    print"Logged In Successfully!"
+1

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


All Articles