xopenex xopenex - 1 month ago 5
Python Question

python login to website, no "id" only "class" for login button

Have been searching and searching, testing and trying, reading and reading... I am not a programmer, and do not pick these things up quickly.

here is my code:

import requests

url = 'http://company.page.com/member/index.php' (this url has the form on it)
values = {'username': 'myusernamehere',
'password': 'mypasswordhere'}

r = requests.post(url, data=values)

# Now you have logged in
url = "http://company.page.com/member/member.php" (this is the resulting url after logging in)

# sending cookies as well
result = requests.get(url, cookies=r.cookies)

print (result.headers)
print (result.text)


I get error:

<form action="http://company.page.com/member/index.php" method="post" id="login">
<h3 class="head">Donor Login</h3>
<em class="notice"><p class="errors">you must log in</p><br />


Image of website source code: (first way i could figure out how to post html)

website sourcecode

Answer

Use session. Or cookies are not kept.

import requests

s = requests.session() # <--

url = 'http://company.page.com/member/index.php'
values = {'username': 'myusernamehere',
          'password': 'mypasswordhere'}

r = s.post(url, data=values) # session.post instead of requests.post

# Now you have logged in
url = "http://company.page.com/member/member.php" (this is the resulting url after logging in)

result = s.get(url) # Use session.get and do not specify cookies.

print (result.headers)
print (result.text)