我无法使用“请求会话”登录该站点

时间:2019-01-17 14:03:31

标签: python beautifulsoup python-requests

我想通过请求会话登录网站。

例如: https://turbobit.net

但是无法正常登录,代码如下:

# 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())

3 个答案:

答案 0 :(得分:1)

BeautifulSoup不会与Web应用程序进行实时交互,它仅将数据作为输入(字符串/字节字符串)并将其解析为格式正确的HTML。

如果要模拟与Web应用程序的交互,例如单击,输入文本和登录。您应该尝试其他选项,例如硒,它是浏览器自动化框架,在这种情况下非常方便。

以下是执行automated login via python script

的示例

答案 1 :(得分:1)

首先找出用于用户名的网站表单上输入内容的名称

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

和密码

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

,并在下面的脚本中替换它们。另外,替换URL以指向所需的站点以登录。

示例代码: 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)

奖金:

要在基于UNIX的系统上从命令行运行此脚本,请将其放在目录home/scripts中,然后将此目录添加到~/.bash_profile中的路径或终端使用的类似文件中。

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

然后在home/scripts/login.py

中创建指向该python脚本的链接
ln -s ~/home/scripts/login.py ~/home/scripts/login

关闭您的终端,启动一个新终端,运行login

答案 2 :(得分:1)

我已经成功登录,代码如下:

# 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)

感谢您的帮助!