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
headers = requests.utils.default_headers()
headers.update(
{
'User-Agent': 'Mozilla/5.0',
}
)
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)
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.