2017-02-18 103 views
4

我试图登录到使用请求的网站,似乎正在打墙。任何意见,将不胜感激。使用Python的请求登录

我试图登录到economist.com(无理由,只是我有一个用户名和密码),其登录页面为https://www.economist.com/user/login,其登录表单的属性为action="https://www.economist.com/user/login?destination=%2F"

使用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和其他隐藏变量登录到登录名;然后使用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']) 

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

回答

1

我试过你的代码,只有1件东西没有与我一起工作。

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!" 
+0

感谢。这工作。我认为我的“表格”工作正常,尽管你的表格不太易破碎。我认为我的问题一直是一个糟糕的测试,看看我是否登录。你的更优雅(虽然/激活重定向我/谢谢你,但前提是相同的)。 – argent656