如何使用mechanize生成验证码图像

时间:2011-04-11 21:43:10

标签: python captcha mechanize

我正在尝试使用python和mechanize从我的移动提供商网站发送短信 问题是表单有一个验证码图像。使用mechanize我可以获得图像的链接,但是当我访问该链接时它总是不同的 有没有办法从机械化中获得精确的图片?

1 个答案:

答案 0 :(得分:11)

这是如何获取图像的一个粗略示例,请注意,mechanize使用cookie,因此收到的任何cookie都将通过图像请求发送到服务器(这可能是您想要的 )。

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提供了很多方法来查找您要查找的标记(请参阅BeautifulSoup docs)。 image_response是一个类似文件的对象。

相关问题