Java实现仿淘宝滑动验证码研究代码详解
程序员文章站
2024-03-12 18:00:26
通过下面一张图看下要实现的功能,具体详情如下所示:
现在我就来介绍些软件的其它功能。希望大家有所受益。
模拟人为搜索商品
在刷单的时候,不能直接拿到一个商...
通过下面一张图看下要实现的功能,具体详情如下所示:
现在我就来介绍些软件的其它功能。希望大家有所受益。
模拟人为搜索商品
在刷单的时候,不能直接拿到一个商品网址就进入购买页面吧,得模拟人为搜索。
在这一个过程中有两个难点:
1)商品列表的异步加载 ; 2)翻页并且截图;
在园子里,我就不在关公面前耍大刀了。
直接上关键代码:
i:搜索商品,并且翻页
public bool? searchproduct(taskdetailmodel taskdetaildata) { bool? result = null; bool isindex = true; bool islist = true; webbrowsertask.instance.setproperties(); webbrowsertask.instance.cleardocumentcompleted(); webbrowsertask.instance.documentcompleted += (wbsendersearch, wbesearch) => { system.windows.forms.webbrowser currentwb = wbsendersearch as system.windows.forms.webbrowser; system.windows.forms.htmldocument currentdoc = currentwb.document; mshtml.htmldocument currentdom = currentdoc.domdocument as mshtml.htmldocument; string wburl = wbesearch.url.tostring(); if (currentwb.readystate == system.windows.forms.webbrowserreadystate.complete) { #region 首页搜索 if (wburl.contains("www.taobao.com")) { if (isindex == true) { isindex = false; taskdetaildata.detailremark = string.format(@"输入关键字""{0}""搜索商品……", taskdetaildata.taskname); func<bool> func = () => { bool asynctag = false; system.threading.thread.sleep(5000); asynctag = true; return asynctag; }; func.begininvoke((ar) => { bool asyncresult = func.endinvoke(ar); if (asyncresult) { system.windows.forms.htmlelement heee = currentdoc.getelementbyid("j_searchtab"); string classname = heee.getattribute("classname"); system.windows.forms.htmlelement hitem = heee.children[0]; system.windows.forms.htmlelementcollection heclis = hitem.children; system.windows.forms.htmlelement li1 = heclis[0]; system.windows.forms.htmlelement li2 = heclis[1]; system.windows.forms.htmlelement li3 = heclis[2]; foreach (system.windows.forms.htmlelement li in heclis) { string liclass = li.getattribute("classname"); if (liclass.contains("selected")) { system.windows.forms.htmlelement q = currentdoc.getelementbyid("q"); system.windows.forms.htmlelement btnsearch = currentdoc.getelementbyid("j_tsearchform").children[0].children[0]; if (q != null && btnsearch != null) { q.focus(); q.setattribute("value", taskdetaildata.taskname); btnsearch.focus(); string savepath = path.combine(userdata.workbenchdirectory, taskdetaildata.taskdetailcode, string.format("搜索提交.jpg", "")); captureimage.capturewebpagearea(currentdom, savepath); btnsearch.invokemember("click"); } } } } }, null); } } #endregion 首页搜索 #region 商品列表 if (wburl.contains("s.taobao.com")) { if (islist == true) { islist = false; func<bool> func = () => { bool asynctag = false; asynctag = true; return asynctag; }; func.begininvoke((ar) => { bool asyncresult = func.endinvoke(ar); if (asyncresult) { //解析每页商品 string clickproducturl = turningandparsepage(currentdoc, taskdetaildata); result = true; } }, null); } } #endregion 商品列表 } }; //documentcompleted结束 system.windows.application.current.dispatcher.invoke(new system.action(() => { webbrowsertask.instance.navigate("https://www.taobao.com/"); })); for (int i = 0; i < 120; i++) { system.threading.thread.sleep(1000); if (result != null) { break; } } return result; }
ii:因为每个页面都是异常加载的,选择适当的时机对网页进行截图
截取整个网页:
/* 因为包含了控件,如果在了线程里调用,必须用invoke方法 system.windows.application.current.dispatcher.invoke(new system.action(() => { //htmldoc.window.scrollto(new system.drawing.point(5000, htmldoc.body.scrollrectangle.height)); string savepath = string.format(@"d:\{0}.jpg", guid.newguid().tostring()); captureimage.capturewebpage(webbrowsertask, savepath); }), system.windows.threading.dispatcherpriority.background); */ /// <summary> /// 截取整个网页 /// </summary> /// <param name="web"></param> /// <param name="savepath"></param> public static void capturewebpage(system.windows.forms.webbrowser web, string savepath) { rectangle body = web.document.body.scrollrectangle; rectangle docrectangle = new rectangle() { location = new point(0, 0), size = new size(body.width, body.height) }; web.dock = dockstyle.none; web.width = docrectangle.width; web.height = docrectangle.height; rectangle imgrectangle = docrectangle; using (bitmap bitmap = new bitmap(imgrectangle.width, imgrectangle.height)) { iviewobject ivo = web.document.domdocument as iviewobject; using (graphics g = graphics.fromimage(bitmap)) { intptr hdc = g.gethdc(); ivo.draw(1, -1, intptr.zero, intptr.zero, intptr.zero, hdc, ref imgrectangle, ref docrectangle, intptr.zero, 0); g.releasehdc(hdc); } bitmap.save(savepath, system.drawing.imaging.imageformat.jpeg); bitmap.dispose(); } }
截取网页的某个区域:
/// <summary> /// 截取网页的一部份 /// </summary> /// <param name="htmldom"></param> /// <param name="savepath"></param> public static void capturewebpagearea(mshtml.htmldocument htmldom, string savepath) { string savedir = system.io.path.getdirectoryname(savepath); if (!system.io.directory.exists(savedir)) { system.io.directory.createdirectory(savedir); } rectangle docrectangle = new rectangle() { location = new point(0, 0), //size = new size(htmldom.body.offsetwidth, htmldom.body.offsetheight) size = new size((int)system.windows.systemparameters.primaryscreenwidth, (int)system.windows.systemparameters.primaryscreenheight) }; rectangle imgrectangle = docrectangle; using (bitmap bitmap = new bitmap(imgrectangle.width, imgrectangle.height)) { iviewobject ivo = htmldom as iviewobject; using (graphics g = graphics.fromimage(bitmap)) { intptr hdc = g.gethdc(); ivo.draw(1, -1, intptr.zero, intptr.zero, intptr.zero, hdc, ref imgrectangle, ref docrectangle, intptr.zero, 0); g.releasehdc(hdc); } bitmap.save(savepath, system.drawing.imaging.imageformat.jpeg); bitmap.dispose(); } }
在这代码里有很多有趣的片段。有心的朋友会发现。