如何使用机械化获取生成的验证码图像 [英] How to get generated captcha image using mechanize
问题描述
我正在尝试使用python和机械化从我的移动服务提供商网站发送短信.
问题在于表单具有验证码图像.使用机械化可以获取图像的链接,但是访问该链接的时间总是不同的.
有什么办法可以从机械化过程中获得准确的照片吗?
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?
推荐答案
这是如何获取图像的粗略示例,请注意,mechanize使用cookie,因此,收到的所有cookie都将随图像请求一起发送到服务器. (这可能是您想要的).
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'
是一个示例,BeautifulSoup
提供了许多查找所需标签的方法(请参见
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.
这篇关于如何使用机械化获取生成的验证码图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!