C#寫爬蟲,版本V1.0

   之前看了Sql Server中的基本數據類型,發現image這個類型還是比較特殊的。

於是乎就做了一個將圖片以二進制流形式存儲的程序http://www.cnblogs.com/JsonZhangAA/p/5568575.html,現在如果我想批量ed存儲網上一個網站的圖片,難道我要手寫n多地址嗎?顯然這是不可取的,針對這種情況,就用C#寫了一個簡單的爬蟲,我們所爬的對象是天文網http://www.tianwenwang.cn/

 

程序的原理是利用WebRequest和WebResponse來相應網站(不懂,只能這樣說0.0),而後利用StreamWrite將網站的源文件存儲到txt文本文件中,這是我們可以發現一個

現象,圖片地址都是類似於http://p.tianwenwang.cn/upload/150318/68181426648163.jpg!list.jpg,http://p.tianwenwang.cn/upload/150312/58341426094069.jpg!list.jpg這種的,於是可以利用正則表達式來將裏面的http:全部取出,放到一個字符串數組中,最後就是判斷地址時候包含典型的jpg,gif等圖片類型後綴了(V1.0最大的缺陷),如果包含就將其存儲到數據庫中。

後臺代碼如下:

複製代碼
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 網絡爬蟲
{
    public partial class Form1 : Form
    {
        private static string[] getLinks(string html)
        {
            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];

            for (int i = 0; i < m.Count; i++)
            {
                links[i] = m[i].ToString(); //提取出結果
            }
            return links;
        }
        private static bool isValiable(string url)
        {
            if (url.Contains(".jpg") || url.Contains(".gif")||url.Contains(".png"))
            {
                return true; //得到一些圖片之類的資源
            }
            return false;
        }
        private static void savePicture(string path)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            Uri url = new Uri(path);
            WebRequest webRequest = WebRequest.Create(url);
            WebResponse webResponse = webRequest.GetResponse();
            
            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();
            }

        }
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string rl;
            string path = this.textBox1.Text;
            Uri url = new Uri(path);
            WebRequest webRequest = WebRequest.Create(url);
            WebResponse webResponse = webRequest.GetResponse();
            Stream resStream = webResponse.GetResponseStream();
            StreamReader sr = new StreamReader(resStream, Encoding.UTF8);
            StringBuilder sb = new StringBuilder();
            while ((rl = sr.ReadLine()) != null)
            {
                sb.Append(rl);
            }
            FileStream aFile = new FileStream("../../txt.txt", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(aFile);//將網頁存儲到了txt文本文件中
            sw.WriteLine(sb.ToString());
            sw.Close();
            string[] s;
            s = getLinks(sb.ToString());
            int i = 0;
            foreach (string sl in s)
            {
                i++;
                savePicture(sl);
            }
        }
    }
}
複製代碼

本版本只能對類似於天文網的這類網站進行爬蟲,我會後續升級爬蟲,爭取做出一個通用的爬蟲O(∩_∩)O~!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章