Java實現仿淘寶滑動驗證碼研究代碼詳解_java

來源:互聯網
上載者:User

通過下面一張圖看下要實現的功能,具體詳情如下所示:

現在我就來介紹些軟體的其它功能。希望大家有所受益。

類比人為搜尋商品

在刷單的時候,不能直接拿到一個商品網址就進入購買頁面吧,得類比人為搜尋。

在這一個過程中有兩個痛點:

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();}} 

在這代碼裡有很多有趣的片段。有心的朋友會發現。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.