2016-09-18 2 views
0

captcha를 png로 저장하는 데 문제가 있습니다. 다음은 이미지 다운로드가 필요하고 새 파일로 로컬에 저장할 지금까지파이썬 저장 url에서 captcha 이미지? 오류 401

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? 
+0

당신은 두 번째 요청 (GET)에 쿠키를 전송해야합니다. 첫 번째 로그인은 세션 쿠키를 반환하기 때문입니다. –

답변

2

내 코드입니다.

다음 샘플 코드입니다 :

import requests 

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

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

UPDATE 의견 후 :

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() 
+0

나는 200 대신에 401 응답 코드를 얻습니다. 어쨌든 저는 이미 인증했습니다. 뭐가 문제 야? –

+0

@DarkCyber ​​여기에 원래 URL을 넣을 수 있습니까? –

+0

이 질문을 업데이트했습니다. –