動態抓取網頁信息

  前幾天在作數據庫實驗時,老是手動的向數據庫中添加少許的固定數據,因而就想如何向數據庫中導入大量的動態的數據?在網上了解了網絡爬蟲,它能夠幫助咱們完成這項工做,關於網絡爬蟲的原理和基礎知識,網上有大量的相關介紹,本人不想在累述,我的以爲下面的文章寫得很是的好(網絡爬蟲基本原理一網絡爬蟲基本原理二)。css

  本博客就以採集博客園首頁的新聞部分爲例吧。本例爲了直觀簡單就採用MVC,將採集到的數據顯示到頁面中,(其實有好多小型網站就是採用抓取技術抓取網上各自須要的信息,再作相應的應用)。另外在實際的抓取過程當中能夠採用多線程抓取,以加快採集的速度。html

  下面咱們先看看博客園的首頁並作相關的分析:web

   採集後的結果:正則表達式

  抓取的原理:先獲取對應url頁面的html內容,而後根據找出你要抓取的目標數據的的html結構,看看這個結構是否有某種規律,而後用正則去匹配這個規則,匹配到了之後就能夠採集出來。咱們能夠先看看頁面源碼,能夠發現新聞部分的規律:位於id="post_list"的<div>之間數據庫

  因而,咱們即可以獲得相應的正則表達式了。網絡

"<div\\s*class=\"post_item\">\\s*.*\\s*.*\\s*.*\\s*.*\\s*.*\\s*.*\\s*.*\\s*<div\\s*class=\"post_item_body\">\\s*<h3><a\\s*class=\"titlelnk\"\\s*href=\"(?<href>.*)\"\\s*target=\"_blank\">(?<title>.*)</a>.*\\s*<p\\s*class=\"post_item_summary\">\\s*(?<content>.*)\\s*</p>"多線程

  原理很簡單,下面我就給出源代碼:線創建一個MVC空項目,再在Controller文件下添加一個控制器HomeController,再爲控制器添加一個視圖Indexpost

HomeController.cs部分代碼:性能

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        /// <summary>
        /// 經過Url地址獲取具體網頁內容 發起一個請求得到html內容
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        public static string SendUrl(string strUrl)
        {
            try
            {
                WebRequest webRequest = WebRequest.Create(strUrl);
                WebResponse webResponse = webRequest.GetResponse();
                StreamReader reader = new StreamReader(webResponse.GetResponseStream());
                string result = reader.ReadToEnd();
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public ActionResult Index()
        {
            string strPattern = "<div\\s*class=\"post_item\">\\s*.*\\s*.*\\s*.*\\s*.*\\s*.*\\s*.*\\s*.*\\s*<div\\s*class=\"post_item_body\">\\s*<h3><a\\s*class=\"titlelnk\"\\s*href=\"(?<href>.*)\"\\s*target=\"_blank\">(?<title>.*)</a>.*\\s*<p\\s*class=\"post_item_summary\">\\s*(?<content>.*)\\s*</p>";
            List<List<string>> list = new List<List<string>>();
            Regex regex = new Regex(strPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.CultureInvariant);
            if (regex.IsMatch(SendUrl("http://www.cnblogs.com/")))
            {
                MatchCollection matchCollection = regex.Matches(SendUrl("http://www.cnblogs.com/"));
                foreach (Match match in matchCollection)
                {
                    List<string> one_list = new List<string>();
                    one_list.Add(match.Groups[2].Value);//獲取到的是列表數據的標題
                    one_list.Add(match.Groups[3].Value);//獲取到的是內容
                    one_list.Add(match.Groups[1].Value);//獲取到的是連接到的地址
                    list.Add(one_list);
                }
            }
            ViewBag.list = list;
            return View();
        }
    }
}

Index視圖部分代碼:網站

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style type="text/css">
        #customers {
            font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
            width: 100%;
            border-collapse: collapse;
            outline: #00ff00 dotted thick;
        }

            #customers td, #customers th {
                font-size: 1em;
                border: 1px solid #98bf21;
                padding: 3px 7px 2px 7px;
            }

            #customers th {
                font-size: 1.1em;
                text-align: left;
                padding-top: 5px;
                padding-bottom: 4px;
                background-color: #A7C942;
                color: #ffffff;
            }
    </style>
</head>
<body>
    <div>
        <table id="customers">
            <tr>
                <th>標題</th>
                <th>內容</th>
                <th>連接</th>
            </tr>
            @foreach (var a in ViewBag.list)
            {
                int count = 0;
                <tr>
                    @foreach (string b in a)
                    {
                        if (++count == 3)
                        {
                            <td><a href="@b">@HttpUtility.HtmlDecode(b)</a></td>@*使轉義符正常輸出*@
                        }
                        else if(count==1)
                        {
                            <td><font color="red">@HttpUtility.HtmlDecode(b)</font></td>
                        }
                        else
                        {
                            <td>@HttpUtility.HtmlDecode(b)</td>
                        }
                    }
                </tr>
            }
        </table>
    </div>
</body>
</html>

  博客寫到這,一個完整的MVC項目就能夠運行了,可是我只採集了一頁,咱們也能夠將博客園首頁中的分頁那部分(即pager_buttom)採集下來,再添加實現分頁的方法便可,在此代碼我就不貼了,本身試試看。不過若是要將信息導入數據庫,則須要創建相應的表,而後按照表中的屬性在從html中一一採集抽取出所須要的相應信息便可,另外咱們不該該將採集到的每條新聞對應的頁面源碼存入數據庫,而應該將每一個新聞對應的連接存入數據庫便可。緣由是下載大量的新聞對應的頁面須要大量的時間,印象採集的效率,而且將大量的新聞頁面文件存入數據庫,會須要大量的內存,還會影響數據庫的性能。

  本人也是個菜鳥,剛學不久,敬請大神們指摘。謝謝。勿笑。。。

 

 

 

相關文章
相關標籤/搜索