• 简易的抓取别人网站内容


    首先,利用到了国内牛人Ivony写的jumony,可在vs中nuget安装

    安装完毕后,我们做个获取博客园首页推荐文章的demo。

    一页有20个推荐文章,我们需要获取他们的标题和内容

    看出都有共同的class,我们可以根据这个获取内容。
      public string Html = string.Empty;
            protected void Page_Load(object sender, EventArgs e)
            {
                string[] number = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty" };
                var htmlSource = new JumonyParser().LoadDocument("http://www.cnblogs.com").Find(".post_item a.titlelnk");
                int count = 0;
                foreach (var htmlElement in htmlSource)
                {
                    count ++;
                    Html += string.Format(" <li>{2}、&nbsp;&nbsp;<a href="About.aspx?Url={0}" target="_blank">{1}</a></li>", htmlElement.Attribute("href").Value(), htmlElement.InnerText(),count);
                }
            }

    Html就是获取完的页面了

    再在aspx中<%=Html%>就可以显示在页面了

    根据上面页面生成的,a链接,我们可以根据这个链接吧内容抓取过来,

      public string Htm2l = string.Empty;
            public string HtmlText2 = string.Empty;
            protected void Page_Load(object sender, EventArgs e)
            {
                string html = Request["Url"];
                var htmlSource = new JumonyParser().LoadDocument(html);
                HtmlText2 = htmlSource.Find(".postTitle2").FirstOrDefault().InnerText();
    
                Htm2l = htmlSource.Find("#cnblogs_post_body").FirstOrDefault().InnerHtml();
            }

    再在页面中引用后台的内容<%=%>就可以把标题的主页显示出来了。

  • 相关阅读:
    设置SSH编码为中文
    深入浅出REST架构 REST架构概述
    RESTful Web Services初探
    Linux 基础命令
    Linux 目录和文件操作
    Linux 压缩文件的命令行总结
    Linxu 监控命令总结
    Linux 下Tomcat的启动、关闭、杀死进程
    Linux日知录(常用问题笔记)
    linux 下远程连接mysql命令详解
  • 原文地址:https://www.cnblogs.com/ithuo/p/4871167.html
Copyright © 2020-2023  润新知