Python urllib2下载验证码图像
我正在尝试使用“机械化”即urllib2从VBB板下载验证码图像。 这是captcha的位置(使用任何用户名登录并通过,您将被询问captcha): 我被改为图片,但它不是验证码图片。 有什么帮助吗 下面是一些来自标题的信息 GET/image.php?type=hv&hash=c76c6f3c2c2e0fc3bf32fd99d3655fa04 HTTP/1.1\r\n接受编码:identity\r\n主机:www.amaderforum.com\r\n主机:bbsessionhash=25e24573ce64dfc95dbb873667f21787;bblastvisit=131264421;bblastactivity=0\r\n连接:关闭\r\n用户代理: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 答复:“HTTP/1.1 200正常\r\n” 标题:日期:2011年8月6日星期六15:30:48 GMT 标题:服务器:Apache 标题:X-Powered-By:PHP/5.2.9 标题:内容传输编码:二进制 标题:内容配置:内联;filename=image.jpg 标题:内容长度:5745 标题:连接:关闭 标题:内容类型:图像/jpegPython urllib2下载验证码图像,python,captcha,urllib2,mechanize,urllib,Python,Captcha,Urllib2,Mechanize,Urllib,我正在尝试使用“机械化”即urllib2从VBB板下载验证码图像。 这是captcha的位置(使用任何用户名登录并通过,您将被询问captcha): 我被改为图片,但它不是验证码图片。 有什么帮助吗 下面是一些来自标题的信息 GET/image.php?type=hv&hash=c76c6f3c2c2e0fc3bf32fd99d3655fa04 HTTP/1.1\r\n接受编码:identity\r\n主机:www.amaderforum.com\r\n主机:bbsessionhash=25e2
下面是一个简短的脚本,它将转到“丢失密码”页面,找到验证码,并将图像下载到out.jpg 此脚本需要库 希望这有帮助。干杯
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)
你能打开下载的.php文件吗?它是ascii码还是二进制码?如果它是一个正确的jpg图像,它应该以神奇的数字-ÿØÿá或类似的东西开始。是的,它是:ÿØÿá,但图像不是验证码图像,它是带有“vBulletin”的图像查看浏览器在看到验证码图像时发送的标题。检查cookies,我猜脚本会发现你不是浏览器,它会向你发送某种默认图像。我现在得到了:)。他们有3秒的超时时间。
br.open('http://www.amaderforum.com/image.php?type=hv&hash=c76c6f3c2e0fc3bf32fd99d36555fa04')
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)