zoukankan      html  css  js  c++  java
  • 网页小说下载

    自己怎么了,改变自己能改变的,比以前好就可以了,一个小说的下载

    命名空间引入(需要导入包HtmlAgilityPack):

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using HtmlAgilityPack;
    View Code

    抓取内容:

    static string m_strTextPath = "D:\333.txt";
            static void Main(string[] args)
            {
                try
                {
                    LoadNovel();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    throw;
                }
                Console.ReadKey();
            }
    
            /// <summary>
            /// 下载导航页面
            /// </summary>
            async
            static void LoadNovel()
            {
                string l_strURL = "http://www.vodtw.com/html/book/34/34009/";
                WebClient wc = new WebClient();
                wc.BaseAddress = l_strURL;
                wc.Encoding = Encoding.GetEncoding("gb2312");
    
                HtmlDocument doc = new HtmlDocument();
                string html = wc.DownloadString("index.html");
    
                doc.LoadHtml(html);
                HtmlNode navNode1 = doc.DocumentNode.SelectSingleNode("/html/body/div[7]/div[5]/dl/dd/ul");
          
                HtmlNodeCollection CNodes1 = navNode1.SelectNodes("child::li");
    
                //解析class 等于下面所有的a标签的URL 存入一个List集合中
                List<string> list = new List<string>();
                foreach (HtmlNode item in CNodes1)
                {
                    list.Add(item.FirstChild.Attributes["href"].Value);
                }
    
                //从集合中读取URL 追加到D盘 222.txt中
                foreach (string l_str in list)
                {
                    string html1 = wc.DownloadString(l_str);
                    //过于频繁的网络请求有问题
                    Thread.Sleep(100);
                    doc.LoadHtml(html1);
    
                    //章节名称 也可以不抓取 再内容里面有
                    //HtmlNode CaptureName = doc.DocumentNode.SelectSingleNode("html/body/div[3]/div[1]/b");
                    //File.AppendAllText("D:\333.txt", CaptureName.InnerText + "
    ");
    
                    //加载内容
                    HtmlNode navNode2 =   doc.DocumentNode.SelectSingleNode("html/body/div[4]/div[4]");
                    HtmlNodeCollection CNodes2 = navNode2.SelectNodes("child::p");
                 
                    foreach (HtmlNode  ddd in CNodes2)
                    {
                        if (ddd.InnerText == "===========================")
                            break;
                        File.AppendAllText(m_strTextPath, ddd.InnerText+"
    ");
                    }
                }
            }
    View Code

    后续需要校验抓取数据的完整性,里面是否有杂乱的东西

  • 相关阅读:
    android 振动
    linux实用命令-查看文件夹的大小
    无显示屏的开发板抓屏
    传感器Sensor的使用-距离感应(听筒模式)
    4.4原生应用获取通话状态
    git服务器的使用
    (转)浅谈ANR及log分析ANR
    ubuntu下从软件中心安装软件时的软件缓存目录
    mysql————Innodb的可重复读的情况下如何避免幻读?
    MyISAM和Innodb的区别
  • 原文地址:https://www.cnblogs.com/maanshancss/p/6979564.html
Copyright © 2011-2022 走看看