找传奇、传世资源到传世资源站!

GeckoWebBrowser采集网络图片实例源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

GeckoWebBrowser采集网络图片实例源码 C#语言基础-第1张using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading;using WindowsFormsApplication1;using Skybound.Gecko;using System.IO;namespace PicDownLoad{ public partial class Form1 : Form { static private string xulrunnerPath = Application.StartupPath "\\xulrunner"; public Form1() { InitializeComponent(); InitAllCityData(); } private void InitAllCityData() { Skybound.Gecko.Xpcom.Initialize(xulrunnerPath); } public static void DownloadOneFileByURL(string fileName, string url, string localPath, int timeout) { try { System.Net.HttpWebRequest request = null; System.Net.HttpWebResponse response = null; request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url fileName); request.Timeout = timeout;//8000 Not work ? response = (System.Net.HttpWebResponse)request.GetResponse(); Stream s = response.GetResponseStream(); BinaryReader br = new BinaryReader(s); //int length2 = Int32.TryParse(response.ContentLength.ToString(), out 0); int length2 = Int32.Parse(response.ContentLength.ToString()); byte[] byteArr = new byte[length2]; s.Read(byteArr, 0, length2); if (File.Exists(localPath fileName)) { File.Delete(localPath fileName); } if (Directory.Exists(localPath) == false) { Directory.CreateDirectory(localPath); } FileStream fs = File.Create(localPath fileName); fs.Write(byteArr, 0, length2); fs.Close(); br.Close(); } catch (Exception ex) { } } /// <summary> ///Web Client Method ,only For Small picture,else large please use FTP /// </summary> /// <param name="fileName"></param> /// <param name="url"></param> /// <param name="localPath"></param> public static void DownloadOneFileByURLWithWebClient(string fileName, string url, string localPath) { try { System.Net.WebClient wc = new System.Net.WebClient(); if (File.Exists(localPath fileName)) { File.Delete(localPath fileName); } if (Directory.Exists(localPath) == false) { Directory.CreateDirectory(localPath); } wc.DownloadFile(url, localPath fileName); } catch (Exception ex) { } } List<string> UrlList = new List<string>(); int AllURL = 0; private void LoadLuoBO() { if (UrlList.Count > 0) { string url = UrlList[0]; ShowProcess(loadURLCount.ToString() "/" AllURL.ToString()); ShowMsg(url); UrlList.Remove(url); this.Browser.Navigate(url); } } int loadURLCount = 0; void Browser_DocumentCompleted_WEIMEI(object sender, EventArgs e) { GeckoWebBrowser br = sender as GeckoWebBrowser; if (br.Url.ToString() == "about:blank") { return; } Skybound.Gecko.GeckoElementCollection imglist = br.Document.GetElementsByTagName("img"); foreach (Skybound.Gecko.GeckoElement a in imglist) { if (a.ClassName != null && a.ClassName.ToLower() == "imgs" ) { try { string hreff = a.Attributes["src"].TextContent;// 排除 博海拾贝第二百零二期】吃完薯条寂寞了 string[] name = hreff.Split('/'); string filenn = ""; if (name.Length > 1) { filenn = name[name.Length - 1]; } else { filenn = Guid.NewGuid().ToString() ".jpg"; } Log.Datalogger(hreff, "ImgList"); DownloadOneFileByURLWithWebClient(filenn, hreff, "d:\\imges\\"); // DownloadOneFileByURL(filenn, hreff, "d:\\imges\\", 5000); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } loadURLCount ; LoadLuoBO(); } #region 固定函数 void AsyncNavigate(string url) { ThreadStart starter = delegate { AsyncNavigateThread(url); }; Thread thread = new Thread(starter); thread.IsBackground = true; thread.Start(); } public void ShowMsg(string msg) { if (this.InvokeRequired) { Action<string> action = this.ShowMsg; this.BeginInvoke(action, msg); } else { this.txtTest.Text = msg; } } public void ShowProcess(string msg) { if (this.InvokeRequired) { Action<string> action = this.ShowProcess; this.BeginInvoke(action, msg); } else { this.txtProcess.Text = msg; } } private delegate void StringDelegate(string url); private void AsyncNavigateThread(string url) { if (this == null || this.IsDisposed) return; if (this.InvokeRequired || Browser.InvokeRequired) { this.BeginInvoke(new StringDelegate(AsyncNavigateThread), new object[] { url }); return; } Browser.Navigate(url); } #endregion private void button1_Click_1(object sender, EventArgs e) { string baseurl = txturl.Text; int begin = int.Parse(txtbegin.Text); int end = int.Parse(txtEnd.Text); for (int i = begin; i <= end; i ) { string replaceurl = baseurl.Replace("(*)", i.ToString()); UrlList.Add(replaceurl); } AllURL = UrlList.Count; //按照顺序去读取界面去 Browser.DocumentCompleted = new EventHandler(Browser_DocumentCompleted_WEIMEI); LoadLuoBO(); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复