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?
Dark Cyber
  • 2,181
  • 7
  • 44
  • 68

1 Answers1

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()
Zulfugar Ismayilzadeh
  • 2,643
  • 3
  • 16
  • 26