1

I want to login to the website by requests sessions.

For example: https://turbobit.net

But can't login normally, the code is as follows:

# coding=utf-8

import lxml
import re
import requests
import socket
import socks
import sys
import time
from bs4 import BeautifulSoup
from urllib.request import urlopen



url = 'https://turbobit.net/user/login'


header = {
    'Host': 'turbobit.net',
    'Referer': 'https://turbobit.net/user/login',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3673.0 Safari/537.36'
}

form_data = {
    'user[login]': 'your_email',
    'user[pass]': 'your_password'
}

session = requests.session()
login = session.post(url, data=form_data, headers=header)
page = session.get('https://turbobit.net/').content
soup = BeautifulSoup(page, features='html.parser')

msgs = soup.find('div', {"class": 'logged'})
for msg in msgs:
    print(msg.get_text())
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
hello123
  • 93
  • 1
  • 8
  • 3
    Your question is entirely wrong. BeautifulSoup actually is a HTML parser, not a HTTP query maker library like requests. – 0xInfection Jan 17 '19 at 14:56
  • I am expressing a mistake, sorry, I mean I am using BeautifulSoup, then I want to login to the website, but I can't login using the code I wrote. – hello123 Jan 18 '19 at 01:25

3 Answers3

1

BeautifulSoup does not interact with web application in realtime, It just takes data as input (string/ byte-string) and parses it as properly formatted HTML.

If you want to simulate interactions with web applications such as clicking, entering text and logging in. You should try other options like selenium which is a Browser Automation Framework and it comes very handy in such cases.

Here's an example to perform automated login via python script

  • The question was misguided, yes, but the context of the problem remains - `requests.session()` should be able to be loaded with a username / password or cookie to fake a logged in session – OneCricketeer Jan 18 '19 at 01:04
  • I am expressing a mistake, sorry, I mean I am using BeautifulSoup, then I want to login to the website, but I can't login using the code I wrote. – hello123 Jan 18 '19 at 01:25
  • Zeeshan Sultan, thank you, i will try it. And thank you, cricket_007. – hello123 Jan 18 '19 at 01:27
1

First of all find out the name of the inputs used on the websites form for usernames 

<form ... name=username ... /> 

and passwords 

<form ... name=password ... /> 

and replace them in the script below. Also replace the URL to point at the desired site to log into.

Example code: login.py

#!/usr/bin/env python 
import requests
payload = { 'username': 'user@email.com', 'password': 'sup3rs3cretp4ssw0rd' } 
url = 'https://example.com/login.html' 
requests.post(url, data=payload, verify=False)

Bonus:

To run this script from the command line on a UNIX based system place it in a directory, i.e. home/scripts and add this directory to your path in ~/.bash_profile or a similar file used by the terminal.

# Custom scripts export
CUSTOM_SCRIPTS=home/scripts
export PATH=$CUSTOM_SCRIPTS:$PATH

Then create a link to this python script inside home/scripts/login.py

ln -s ~/home/scripts/login.py ~/home/scripts/login

Close your terminal, start a new one, run login.

0xInfection
  • 2,676
  • 1
  • 19
  • 34
1

I have successfully logged in, the code is as follows:

# coding=utf-8

import lxml
import re
import requests
import socket
import socks
import sys
import time
from bs4 import BeautifulSoup
from urllib.request import urlopen
from requests import Request, Session


email = "your_email"
password = "yor_password"

s = requests.Session()

user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3673.0 Safari/537.36"
headers = {
    'Authority':'turbobit.net',
    'Method':'POST',
    'Path':'/lang/en',
    'Host': 'turbobit.net',
    'Referer': 'https://turbobit.net/login',
    'User-Agent': user_agent
}


def login_site(email, password):
    login_url = 'https://turbobit.net/user/login'
    form_data = {
        'user[login]': email,
        'user[pass]': password,
        'user[submit]':'Sign in',
        'user[memory]':'on'
        }
    login = s.post(login_url, data=form_data, headers=headers)

    # print(f"text = {login.text}")
    soup = BeautifulSoup(login.text, "lxml")
    '''If the login is successful, there will be "<div class="lang-links in">" in the output.'''
    msgs = soup.find('div', {"class": "user-menu"})
    print(msgs)



login_site(email, password)

Thanks you for your help!

hello123
  • 93
  • 1
  • 8