Nishant Kashyap
Nishant Kashyap

Reputation: 829

Sumit captcha and other values using Urllib or Urllib2 python

How can I Submit the form which has a Captch Image in it ? Tried this code

import urllib
import urllib2
from PIL import Image
import pytesser
#include the pytesser into the site pacakges 
#and run sudo apt-get tesseract-ocr it is required by the
#pytesser to run the image converter 

image = urllib.URLopener()
image.retrieve("http://www.stat.gov.pl/regon/Captcha.jpg","Captcha.jpg")
#The image get saved into current script directory
image = Image.open('Captcha.jpg')
print image_to_string(image) #I will get the text from the Captcha Image
text=image_to_string(image)

Now I wanted to send this data dict to the opened request in the post so to get the next page consist of the details

data={'criterion1TF':5213510101,'verifCodeTF':text}

but when I use the urllib.URLopener() It again opens the new page which has the different captcha image. I hope any one could help me to solve this. Thanks in advance.

Upvotes: 1

Views: 2060

Answers (1)

paj28
paj28

Reputation: 2320

This is probably because you're not saving cookies. urllib2 can help you with this; if you setup your opener like this:

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

Then it will transparently save and submit cookies.

Upvotes: 1

Related Questions