1

I have a problem to save captcha as png. Here is my code so far

import requests
url_login = "https://www.hackthis.co.uk/?login"
r_login = requests.post(url_login, {'username': 'darkcyber', 'password': 'secr3tp4s5'})
print r_login.status_code
if "Invalid login details" in r_login.text:
        print "Failed to login"
else:
        print "Login success"
url_captcha = "https://www.hackthis.co.uk/levels/extras/captcha1.php"
r_captcha = requests.get(url_captcha)
#print r_captcha.status_code << 401 instead 200
#whats next?
1
  • You need send cookies in second request (GET). Because first login returns session cookies. Commented Sep 19, 2016 at 6:08

1 Answer 1

4

You need download image and save to local as new file.

Here's the sample code:

import requests

r = requests.get('http://url.com/captcha.php')

f = open('yourcaptcha.png', 'wb')
f.write(r.content)
f.close()

UPDATE after your comments:

import requests
url_login = "https://www.hackthis.co.uk/?login"

r_login = requests.post(url_login, {'username': 'darkcyber', 'password': 'secr3tp4s5'})

if "Invalid login details" in r_login.text:
        print "Failed to login"
else:
        print "Login success"

url_captcha = "https://www.hackthis.co.uk/levels/extras/captcha1.php"
r_captcha = requests.get(url_captcha, cookies=r_login.history[0].cookies)

print r_captcha.status_code

f = open('yourcaptcha.png', 'wb')
f.write(r_captcha.content)
f.close()
Sign up to request clarification or add additional context in comments.

6 Comments

I get 401 response code instead 200, anyway I have already authenticated. Whats wrong?
@DarkCyber can you put here the original URL?
updated the question.
@DarkCyber updated answer.
It works :D, anyway can you tell me about cookies=r_login.history[0].cookies more clear, I dont see in documentation. confuse with history[0]
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.