這次是對2.0的小修補,2.0交互幾乎沒有,這次添加了進度條,和文本框,同時由於取得的鏈接主要會出現錯誤是:webResponse錯誤。 針對這種情況,設置了 截取錯誤信息,這裡我們不處理,後續直接判定statecode屬性來決定是否還要執行下麵的程式。 另外一點變化就是以前是通過將所獲取的網頁存到 ...
這次是對2.0的小修補,2.0交互幾乎沒有,這次添加了進度條,和文本框,同時由於取得的鏈接主要會出現錯誤是:webResponse錯誤。
針對這種情況,設置了
try { webResponse = (HttpWebResponse)webRequest.GetResponse(); } catch(WebException ex) { webResponse = (HttpWebResponse)ex.Response; }
截取錯誤信息,這裡我們不處理,後續直接判定statecode屬性來決定是否還要執行下麵的程式。
另外一點變化就是以前是通過將所獲取的網頁存到文本中去,這次
WebRequest myRequest = WebRequest.Create("http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1466307565574_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=" + Uri.EscapeDataString(keyWord)); HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); if (myResponse.StatusCode == HttpStatusCode.OK) { Stream strm = myResponse.GetResponseStream(); StreamReader sr = new StreamReader(strm); string line = sr.ReadToEnd();
將它全放入了string中。
最後一點是去掉了DownloadPage這個方法,如上,它的功能可以放入按鈕的單擊事件中實現,沒有必要把一件事做兩遍。
下麵是前臺頁面:
後臺代碼:
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; namespace 百度圖片爬蟲V2._1 { public partial class Form1 : Form { public delegate void AsynFunction(string s,int i); public Form1() { InitializeComponent(); } private static string[] getLinks(string html, out int counts) { const string pattern = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"; Regex r = new Regex(pattern, RegexOptions.IgnoreCase); //新建正則模式 MatchCollection m = r.Matches(html); //獲得匹配結果 string[] links = new string[m.Count]; int count = 0; for (int i = 0; i < m.Count; i++) { if (isValiable(m[i].ToString())) { links[count] = m[i].ToString(); //提取出結果 count++; } } counts = count; return links; } private void button1_Click(object sender, EventArgs e) { string keyWord = this.textBox1.Text; WebRequest myRequest = WebRequest.Create("http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1466307565574_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=" + Uri.EscapeDataString(keyWord)); HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); if (myResponse.StatusCode == HttpStatusCode.OK) { Stream strm = myResponse.GetResponseStream(); StreamReader sr = new StreamReader(strm); string line = sr.ReadToEnd(); int counts = 0; string[] str = getLinks(line, out counts); this.progressBar1.Maximum = counts; for (int i = 0; i < counts; i++) { AsynFunction fun = new AsynFunction(savePicture); fun.BeginInvoke(str[i],i, ar => { fun.EndInvoke(ar); this.progressBar1.BeginInvoke(new Action(() => { this.progressBar1.Value =progressBar1.Maximum; })); this.textBox2.BeginInvoke(new Action(() => { StringBuilder sb=new StringBuilder(); sb.Append(Environment.NewLine); // sb.Append(str[i].ToString()); sb.Append("下載結束"); this.textBox2.Text += sb.ToString(); })); }, fun); } } } private static bool isValiable(string url) { if (url.Contains(".jpg") || url.Contains(".gif") || url.Contains(".png")) { return true; //得到一些圖片之類的資源 } return false; } public void savePicture(string path,int i) { if (path != "" && path != null) { DataClasses1DataContext db = new DataClasses1DataContext(); Uri url = new Uri(path); HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url); webRequest.Referer = "http://image.baidu.com"; webRequest.Timeout = 30000; //設置連接超時時間 webRequest.AllowAutoRedirect = true; webRequest.Headers.Set("Pragma", "no-cache"); webRequest.UserAgent = "Mozilla-Firefox-Spider(Wenanry)"; HttpWebResponse webResponse; try { webResponse = (HttpWebResponse)webRequest.GetResponse(); } catch(WebException ex) { webResponse = (HttpWebResponse)ex.Response; } if(webResponse!=null&&webResponse.StatusCode==HttpStatusCode.OK) { if (isValiable(path))//判斷如果是圖片,就將其存儲到資料庫中。 { Bitmap myImage = new Bitmap(webResponse.GetResponseStream()); MemoryStream ms = new MemoryStream(); myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); var p = new pictureUrl { pictureUrl1 = ms.ToArray() }; db.pictureUrl.InsertOnSubmit(p); db.SubmitChanges(); this.progressBar1.BeginInvoke(new Action(() => { this.progressBar1.Value = i; })); this.textBox2.BeginInvoke(new Action(() => { StringBuilder sb1 = new StringBuilder(); sb1.Append(path); sb1.Append("圖片下載開始" + Environment.NewLine); this.textBox2.Text += sb1.ToString(); })); } } } } private void button2_Click(object sender, EventArgs e) { this.Close(); } } }