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

相关文章
相关标签/搜索