python - urllib2 下载验证码图片

标签 python captcha urllib2 mechanize urllib

我正在尝试使用“Mechanize ”又名 urllib2 从 VBB 板下载验证码图像。
这是验证码所在的位置(使用任何用户名登录并通过,您将被要求验证码):

<img id="imagereg" src="image.php?type=hv&amp;hash=c76c6f3c2e0fc3bf32fd99d36555fa04" alt="" width="201" height="61" border="0" />

我试图检索该图像,但它下载了一个 .php 文件
br.open('http://www.amaderforum.com/image.php?type=hv&amp;hash=c76c6f3c2e0fc3bf32fd99d36555fa04')

我将 ext 名称更改为图像,但它不是验证码图像。
有什么帮助吗?

以下是来自标题的一些信息

GET /image.php?type=hv&hash=c76c6f3c2e0fc3bf32fd99d36555fa04 HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: www.amaderforum.com\r\nCookie: bbsessionhash=25e24573ce64dfc95dbb873667f21787; bblastvisit=1312644421; bblastactivity=0\r\nConnection: close\r\nUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.17) Gecko/20110420 Firefox/3.6.17\r\n\r\n' reply: 'HTTP/1.1 200 OK\r\n' header: Date: Sat, 06 Aug 2011 15:30:48 GMT

header: Server: Apache

header: X-Powered-By: PHP/5.2.9

header: Content-transfer-encoding: binary

header: Content-disposition: inline; filename=image.jpg

header: Content-Length: 5745

header: Connection: close

header: Content-Type: image/jpeg

最佳答案

这是一个简短的脚本,可以转到丢失的密码页面,找到验证码,并将图像下载到 out.jpg。

此脚本需要 lxml图书馆。

希望这可以帮助。干杯!

import urllib2
import lxml.html

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/2010010' \
    '1 Firefox/4.0.1',
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language':'en-us,en;q=0.5',
    'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.7'}

req = urllib2.Request('http://www.amaderforum.com/login.php?do=lostpw', None,
                      headers)
f = urllib2.urlopen(req)
page = f.read()

tree = lxml.html.fromstring(page)
imgurl = "http://www.amaderforum.com/" + \
      tree.xpath(".//img[@id='imagereg']")[0].get('src')

req = urllib2.Request(imgurl, None, headers)
f = urllib2.urlopen(req)
img = f.read()

open('out.jpg', 'wb').write(img)

关于python - urllib2 下载验证码图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6967908/

相关文章:

django - 易于使用的 django 验证码或带有验证码的注册应用程序?

python - 带有 cookie 的 urllib2

python - python urllib2中的自定义方法

python - 将补丁添加到在 matplotlib 中打开的 numpy 数组

python - K-means 颜色聚类 - 使用屏蔽 numpy 数组省略背景像素

python - 如何在 python 中定义一个包含 1000 位数字的十进制类?

python - 如何使用 tensorflow 有效地提取给定长度的所有切片

captcha - 将 Google reCaptcha 版本 1 从 2 切换

python - 哪个更好地测试 python 中的有效 url

javascript - ReCaptcha API v2 样式