使用Python的请求登录

我试图使用请求登录到一个网站,似乎正在撞墙.任何意见,将不胜感激.

我正在尝试登录到economist.com(没有理由,只是我有用户名和密码),其登录页面位于https://www.economist.com/user/login,其登录表单中包含attribute action =“https://www.economist.com/user/login?destination=/”.

使用Chrome开发人员工具,登录请求的表单数据如下:

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

我的代码获取登录页面,使用BeautifulSoup来确定form_id;尝试使用我的用户名和密码,检索到的form_id和其他隐藏变量POST到登录名;然后使用BeautifulSoup检查主页,看看横幅是否有登录或注销链接,以确定我是否实际登录过.

代码如下:

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'])

运行时,此代码显示横幅仍然具有登录链接而不是注销链接,我认为这意味着它没有登录.我知道我必须在这里犯了一些非常简单的错误,但在阅读其他内容之后类似的问题在这里,我似乎无法找到我出错的地方.我很欣赏任何有关这项工作的建议.

最佳答案 我尝试了你的代码,只有一件事与我无关.

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

至:

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!"
点赞