How to get generated captcha image using mechanize

北战南征 提交于 2019-12-03 10:39:15

问题


I'm trying to use python and mechanize to send sms from my mobile provider website.
The problem is that form has a captcha image. Using mechanize I can get the link to the image, but it's different all the time I access that link.
Is there any way to get exact picture from mechanize?


回答1:


This is a rough example of how to get the image, note that mechanize uses cookies so any cookies received will be sent to the server with the request for the image (this is probably what you want).

br = mechanize.Browser()
response = br.open('http://example.com')
soup = BeautifulSoup(response.get_data())
img = soup.find('img', id='id_of_image')
image_response = br.open_novisit(img['src'])
image = image_response.read()

id='id_of_image' is an example, BeautifulSoup provides many ways to find the tag you're looking for (see the BeautifulSoup docs). image_response is a file-like object.



来源:https://stackoverflow.com/questions/5627923/how-to-get-generated-captcha-image-using-mechanize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!