user1928392
user1928392

Reputation: 55

unable to log in to a website with requests

I have been trying for ages to log in to https://angel.co/users/login using pythons requests module. The problem is that the the I get a 404 status code back every time I run the code, even though I know the url exists. I think the problem is that the form in the url has an action attribute with a relative link. I have no idea how to fix this and I have had no luck searching for a solution.

Here is the code i'm using:

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

Here is the code that I have ended up using:

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

Thanks Thothadri and Blackjack.

Upvotes: 4

Views: 1586

Answers (1)

Thothadri Rajesh
Thothadri Rajesh

Reputation: 522

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

Upvotes: 2

Related Questions