简体   繁体   English

使用Python的请求登录

[英]Logging in using Python's Requests

I am attempting to log in to a website using Requests and seem to be hitting a wall. 我试图使用请求登录到一个网站,似乎正在撞墙。 Any advice would be appreciated. 任何意见,将不胜感激。

I'm attempting to log in to economist.com (no reason, just something I have a username and password for), whose login page is at https://www.economist.com/user/login and whose login form has the attribute action="https://www.economist.com/user/login?destination=%2F" . 我正在尝试登录到economist.com(没有理由,只是我有用户名和密码),其登录页面位于https://www.economist.com/user/login ,其登录表单中包含attribute action="https://www.economist.com/user/login?destination=%2F"

Using the Chrome developer tools, the form data for a login request is as follows: 使用Chrome开发人员工具,登录请求的表单数据如下:

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

My code GETs the login page, uses BeautifulSoup to determine the form_id; 我的代码获取登录页面,使用BeautifulSoup来确定form_id; attempts to POST to the login using my username and password, the retrieved form_id, and the other hidden variables; 尝试使用我的用户名和密码,检索到的form_id和其他隐藏变量POST到登录名; and then uses BeautifulSoup to check the homepage to see if the banner has a login or logout link to determine if I have actually logged in. 然后使用BeautifulSoup检查主页,看看横幅是否有登录或注销链接,以确定我是否实际登录过。

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 the login link instead of the logout link, which, I assume, means that it's not logged in. I know I must have made some very simple mistake in here, but after reading through other similar questions on here, I can't seem to find where I'm going awry. 运行时,此代码显示横幅仍然具有登录链接而不是注销链接,我认为这意味着它没有登录。我知道我必须在这里犯一些非常简单的错误,但在阅读其他内容之后类似的问题在这里,我似乎无法找到我出错的地方。 I'd appreciate any advice on making this work. 我很欣赏任何有关这项工作的建议。

I tried your code and only 1 thing did not work with me. 我尝试了你的代码,只有一件事与我无关。

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

To: 至:

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

Then login normally, and to make sure if am logged in or not, i get a page that only logged in users can visit. 然后正常登录,并确保是否登录,我得到一个只登录用户可以访问的页面。 http://www.economist.com/subscriptions/activation http://www.economist.com/subscriptions/activation

if you can visit this page, then you are logged in, or you will be redirected to https://www.economist.com/user/register?destination=subscriptions%2Factivation&rp=activating 如果您可以访问此页面,那么您已登录,或者您将被重定向到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!"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM