C# 爬蟲 Jumony-html解析

前言

  前幾天寫了個爬蟲,而後認識到了本身的不足。 烽火情懷推薦了Jumony.Core,經過倚天照海- -推薦的文章,也發現了Jumony.Core。html

  研究了2天,我發現這個東西簡單粗暴,很是好用,由於語法比較像jQuery。上手快,也很好理解。git

添加DLL

  IDE是Visual Studio 2013,我是在NugGet中搜索,並添加到項目中。github

  

Jumony的用法

一、從網站獲取html代碼,將html字符串分析爲標準的文檔對象模型(DOM)。post

IHtmlDocument source = new JumonyParser().LoadDocument("http://www.23us.so/files/article/html/13/13655/index.html", System.Text.Encoding.GetEncoding("utf-8"));

Jumony的API能夠從互聯網上直接抓取文檔分析,並根據HTTP頭自動識別編碼,可是上面的網站怎麼也沒法獲取到html,其餘網站就沒問題(例如博客園、起點),後來我把源碼下載下來,一步步測試,發現html是獲取到的,可是亂碼,致使了Jumony類庫分析html文本的時候,分析的不正確。解決辦法就是設置utf-8。測試

二、獲取全部的meta標籤網站

var aLinks = source.Find("meta");//獲取全部的meta標籤
foreach (var aLink in aLinks)
{
    if (aLink.Attribute("name").Value() == "keywords")
    {
        name = aLink.Attribute("content").Value();//無疆,無疆最新章節,無疆全文閱讀
    }
}

三、獲取 name=keywords 的meta標籤,並獲得content屬性裏的值編碼

string name = source.Find("meta[name=keywords]").FirstOrDefault().Attribute("content").Value();

四、獲取全部Class=Lurl

var lLinks = source.Find(".L");//獲取全部class=L的td標籤
foreach (var lLink in lLinks)//循環class=L的td
{ 
  //lLink值 例如:<td class="L"><a href="http://www.23us.so/files/article/html/13/13655/5638724.html">楔子</a></td>  
}

var aLinks = source.Find(".L a");//獲取全部class=L下的a標籤
foreach (var aLink in aLinks)
{   
  //aLink值 <a href="http://www.23us.so/files/article/html/13/13655/5638724.html">楔子</a>
  string title = aLink.InnerText()//楔子 
  string url = aLink.Attribute("href").Value();//http://www.23us.so/files/article/html/13/13655/5638724.html
}

五、根據ID獲取spa

var chapterLink = source.Find("#at a");//查找id=at下的全部a標籤
foreach (var i in chapterLink)//這裏循環的就是a標籤
{ 
  //aLink值 例如:<a href="http://www.23us.so/files/article/html/13/13655/5638724.html">楔子</a> 
  string title = i.InnerText();//楔子
  string url = i.Attribute("href").Value();//http://www.23us.so/files/article/html/13/13655/5638724.html 
} 

C#完整代碼

using Ivony.Html;
using Ivony.Html.Parser;using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace Test.Controllers
{
    public class CrawlerController : BaseController
    {
        // GET: Crawler
        public void Index()
        {
            //須要給utf-8的編碼,不然html是亂碼。
            IHtmlDocument source = new JumonyParser().LoadDocument("http://www.23us.so/files/article/html/13/13655/index.html", System.Text.Encoding.GetEncoding("utf-8"));

            //<meta name="keywords" content="無疆,無疆最新章節,無疆全文閱讀"/>
            string name = source.Find("meta[name=keywords]").FirstOrDefault().Attribute("content").Value().Split(',')[0];//獲取小說名字
            var chapterLink = source.Find("#at a");//查找id=at下的全部a標籤
            foreach (var i in chapterLink)//這裏循環的就是a標籤
            {
                //章節標題
                string title = i.InnerText();

                //章節url
                string url = i.Attribute("href").Value();

                //根據章節的url,獲取章節頁面的html
                IHtmlDocument sourceChild = new JumonyParser().LoadDocument(url, System.Text.Encoding.GetEncoding("utf-8"));

                //查找id=contents下的小說正文內容
                string content = sourceChild.Find("#contents").FirstOrDefault().InnerHtml().Replace("&nbsp;", "").Replace("<br />", "\r\n");

                //txt文本輸出
                string path = AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "/") + "Txt/";
                Novel(title + "\r\n" + content, name, path);
            }
        }
    }
}

 


相關文章:C# 爬蟲 抓取小說code

Jumony源代碼地址:https://github.com/Ivony/Jumony

相關文章
相關標籤/搜索