碰到一个很棘手问题,请求高手帮助,如何保存网页中的图片为文件? Topic is solved

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: tmplinshi, arcticir

gongnl
Posts: 95
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

碰到一个很棘手问题,请求高手帮助,如何保存网页中的图片为文件?

17 Sep 2016, 08:19

我想制作一个验证码图片识别,不知如何将网页中的验证码图片保存为图片文件,通过getElementbyId已经提取到图片的<img src="...">,如果使用urlDownloadtoFile 再次去获取图片,识别出来的验证码就不匹配了,请教高手如何直接保存网页中的验证码图片,不要再次向服务器获取?请指教多谢了!
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 碰到一个很棘手问题,请求高手帮助,如何保存网页中的图片为文件?

17 Sep 2016, 08:27

WBImg 函数 -- https://autohotkey.com/boards/viewtopic.php?f=6&t=10797

示例代码:

Code: Select all

img := wb.document.getElementById("imgVerifycode")
WBImg.ToFile("vcode.png", img)
gongnl
Posts: 95
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: 碰到一个很棘手问题,请求高手帮助,如何保存网页中的图片为文件?

17 Sep 2016, 12:03

tmplinshi wrote:WBImg 函数 -- https://autohotkey.com/boards/viewtopic.php?f=6&t=10797

示例代码:

Code: Select all

img := wb.document.getElementById("imgVerifycode")
WBImg.ToFile("vcode.png", img)
非常感谢您的指教,我通过AHKinfo提取的图片元素信息如下:
<IMG onclick=refreshVerifyCode(this); id=checkCode title=点击重新获取验证码 src="..../code.jsp">

运行代码

Code: Select all

Pwb := WBGet()
img := Pwb.document.getElementById("checkCode")
WBImg.ToFile("codefile.png", img)
运行出现错误提示如下,而wb_execEle(img, "copy") 则可正常运行,我要怎么解决,多谢了
---------------------------
Error in #include file "C:\AutoHotkey\Asone\WBImg.ahk":
0x80020006 - 未知名称。


Specifically: getContext

Line#
647: DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
648: Return,ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
649: }
650: }
651: }
026: {
027: cnv := imgEle.ownerDocument.createElement("CANVAS")
028: cnv.width := imgEle.naturalWidth
029: cnv.height := imgEle.naturalHeight
---> 030: ctx := cnv.getContext("2d")
031: ctx.drawImage(imgEle, 0, 0)
032: base64Url := cnv.toDataURL(mimeType, jpegQuality)
033: }
035: {
036: this.ToBase64Url(base64Url, imgEle, mimeType, jpegQuality)

Continue running the script?
---------------------------
是(Y) 否(N)
---------------------------
Last edited by gongnl on 17 Sep 2016, 12:24, edited 1 time in total.
gongnl
Posts: 95
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: 碰到一个很棘手问题,请求高手帮助,如何保存网页中的图片为文件?

17 Sep 2016, 23:27

tmplinshi wrote:IE 版本至少要 9 或以上。
我是win7和ie10环境下运行出错
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 碰到一个很棘手问题,请求高手帮助,如何保存网页中的图片为文件?

18 Sep 2016, 00:01

你测试下 https://www.baidu.com/ 是否正常

Code: Select all

wb := wbget()
img := wb.document.all.tags("IMG")[0]
WBImg.ToFile("logo.png", img)
gongnl
Posts: 95
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: 碰到一个很棘手问题,请求高手帮助,如何保存网页中的图片为文件?

18 Sep 2016, 02:54

运行出现错误提示如下
---------------------------
Error in #include file "C:\AutoHotkey\Asone\WBImg.ahk":
0x80020101 -
Specifically:toDataURL

测试了几个脚本出现不同的错误地方,第一次是naturalWidth,第二次是getContext("2d"),现在是toDataURL
很奇怪,而wb_execEle(img, "copy") 则可正常运行
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 碰到一个很棘手问题,请求高手帮助,如何保存网页中的图片为文件?  Topic is solved

18 Sep 2016, 03:55

不知道什么问题。

可以复制图片的话,那么可以用 GDIP.ahk 保存剪贴板图片到文件。示例代码:

Code: Select all

wb := wbget()
img := wb.document.all.tags("IMG")[0]

ClipBackup := ClipboardAll
Clipboard := ""
wb_copy(img)
ClipWait, 2

if ErrorLevel {
	MsgBox, 48, 错误, 复制图片失败
} else {
	ClipImgToFile("img.png")
}

Clipboard := ClipBackup
ClipBackup := ""
return

ClipImgToFile(sOutput, Quality:=75) {
	pToken := Gdip_Startup()
	pBitmap := Gdip_CreateBitmapFromClipboard()
	Gdip_SaveBitmapToFile(pBitmap, sOutput, Quality)
	Gdip_DisposeImage(pBitmap)
	Gdip_Shutdown(pToken)
}

wb_copy(ele) {
	ctrlRange := ele.ownerDocument.body.createControlRange()
	ctrlRange.add(ele)
	ctrlRange.execCommand("copy")
}
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 碰到一个很棘手问题,请求高手帮助,如何保存网页中的图片为文件?

18 Sep 2016, 04:33

我刚测试了一个网站 http://uuwise.com/apiUserLogin.html,也是提示 getContext 错误。按 F12 打开开发人员工具,发现文档模式用的是 7

Image

点击下拉菜单选择 9,再测试保存图片就正常了。
gongnl
Posts: 95
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: 碰到一个很棘手问题,请求高手帮助,如何保存网页中的图片为文件?

18 Sep 2016, 05:05

F12键显示的标准的页面模式,使用你上面提供的代码成功了,再次感谢!WBimg 不是很完善执行不成功。

Return to “请求帮助”

Who is online

Users browsing this forum: No registered users and 83 guests