2015-08-26 154 views
0

我一直在尝试使用pythons请求模块登录到https://angel.co/users/login。问题是我每次运行代码时都会返回一个404状态码,即使我知道url存在。 我认为问题在于url中的表单有一个相对链接的action属性。 我不知道如何解决这个问题,我没有运气寻找解决方案。无法登录到请求的网站

这里是我使用的代码:

import requests 

with requests.Session() as s: 
    url = 'https://angel.co/users/login' 
    payload = { 'user[email]' : 'username_here', 
       'user[password]' : 'password_here'}   

    r = s.post(url, data=payload) 
    print r.status_code # This is printing 404 

这里是我已经结束了使用的代码:

import requests 
from bs4 import BeautifulSoup 
from login_details import username, password # This is just a script with my username and password 

s=requests.session() 

main_url="https://angel.co/login?utm_source=top_nav_home" 

s.headers = {'Content-Type'    : 'application/x-www-form-urlencoded', 
      'Host'      : 'angel.co', 
      'Origin'     : 'https://angel.co', 
      'User-Agent'    : 'Mozilla/5.0 (Windows NT 6.1; WOW64)' \ 
              'AppleWebKit/537.36 (KHTML, like Gecko) ' \ 
              'Chrome/44.0.2403.157 ' \ 
              'Safari/537.36', 
      'Referer'     : main_url, 
      'Upgrade-Insecure-Requests' : '1'} 

response = s.get(main_url) 
soup = BeautifulSoup(response.content) 

payload={'login_only'   : 'true', 
     'user[email]'  : username, 
     'user[password]'  : password, 
     'authenticity_token' : soup.find(attrs={'name' : 'authenticity_token'})['value'], 
     'utf8'    : '%25E2%259C%2593'} 
#the value of utf8 gets urlencoded once you send the request. 

response = s.post("https://angel.co/users/login", data = payload) 
print response.status_code 

感谢Thothadri和酒杯。

+1

有一个csrf令牌的形式,你应该在'post' – mshsayem

+0

期间提供嘿,我刚刚通过网站,你给不完整的信息。 Payload需要以下信息“真实性标记”,“login_only”,“utf-8”。我建议你在铬中使用检查元素,并理解正确的请求 –

+0

嗯,我也这么做。但重点是,他需要动态的csrf令牌;而其他则是静态的(邮件和通行证除外)。 – mshsayem

回答

0
import re,requests 

s=requests.session() 

main_url="https://angel.co/login?utm_source=top_nav_home" 
headers={"Content-Type":"application/x-www-form-urlencoded","Host":"angel.co","Origin":"https://angel.co"\ 
,"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"} # Mostly you need to pass the headers . Default headers don't work always. So be careful here 

r1=s.get(main_url,headers=headers) #authenticity token is extracted from this page using the regex i mentioned below. This is required when you login to the page 
token=re.search("""<input name="authenticity_token" type="hidden" value="(.*?)"[^>]*?>""",r1.text,re.S|re.I) 
print token.group(1) 
headers["Referer"]="https://angel.co/login?utm_source=top_nav_home" 
headers["Upgrade-Insecure-Requests"]="1" 
payload={"login_only":"true","user[email]":"youremail","user[password]":"yourpassword","authenticity_token":token.group(1),"utf8":"%25E2%259C%2593"} # the value of utf8 gets urlencoded once you send the request. 
r2=s.post("https://angel.co/users/login",headers=headers,data=payload,cookies=r1.cookies) 
print r2.status_code 
+0

这可能会更清洁。 'Session'应该自动处理cookie,并且还可以为会话对象上的所有请求设置标题。定义了'main_url',但引用标头由相同的文字字符串设置。 – BlackJack

+0

谢谢Thothadri。这工作完美。我做了BlackJack建议的更改,我使用bs4而不是re来获取authenticity_token。 – user1928392

+0

@ user1928392 - 你可以标记答案,因为它的工作? –