使用Python';s请求

使用Python';s请求,python,login,python-requests,Python,Login,Python Requests,我试图使用请求登录到一个网站,似乎遇到了麻烦。如有任何建议,将不胜感激 form = login.find('input', attrs={'name': "form_build_id"}) 我正在尝试登录economist.com(没有原因,只是我有用户名和密码),其登录页面位于https://www.economist.com/user/login并且其登录表单具有属性action=”https://www.economist.com/user/login?destination=%2F“

我试图使用请求登录到一个网站,似乎遇到了麻烦。如有任何建议,将不胜感激

form = login.find('input', attrs={'name': "form_build_id"})
我正在尝试登录economist.com(没有原因,只是我有用户名和密码),其登录页面位于
https://www.economist.com/user/login
并且其登录表单具有属性
action=”https://www.economist.com/user/login?destination=%2F“

form = login.find('input', attrs={'name': "form_build_id"})
使用Chrome developer工具,登录请求的表单数据如下所示:

name: ///////// 
pass: ////////
form-build-id: form-483956e97a61f73fbc0ebf06b04dbe3f
form_id: user_login
securelogin_original_baseurl: https://www.economist.com
op: Log in
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.find('input', attrs={'name': "form_build_id"})
我的代码获取登录页面,使用BeautifulSoup确定表单id;尝试使用我的用户名和密码、检索到的表单id和其他隐藏变量发布到登录;然后使用BeautifulSoup检查主页,查看横幅是否有登录或注销链接,以确定我是否实际登录

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

name: ///////// 
pass: ////////
form-build-id: form-483956e97a61f73fbc0ebf06b04dbe3f
form_id: user_login
securelogin_original_baseurl: https://www.economist.com
op: Log in
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.find('input', attrs={'name': "form_build_id"})

当运行时,这段代码显示横幅仍然有登录链接而不是注销链接,我认为这意味着它没有登录。我知道我在这里肯定犯了一些非常简单的错误,但在阅读了这里的其他类似问题之后,我似乎找不到我的错误所在。如果您能给我一些建议,我将不胜感激。

我尝试了您的代码,但只有一件事对我不起作用

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

form = login.find('input', attrs={'name': "form_build_id"})
然后正常登录,为了确保我是否登录,我会得到一个只有登录用户才能访问的页面。

form = login.find('input', attrs={'name': "form_build_id"})
如果您可以访问此页面,则您已登录,或者您将被重定向到

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

谢谢这很有效。我想我的
表单
是有效的,尽管你的表单不太容易损坏。我认为我的问题一直是一个糟糕的测试,看看我是否登录。你的更优雅(虽然/activation将我重定向到/thankyou,但前提是一样的)。