zoukankan      html  css  js  c++  java
  • 简易的抓取别人网站内容

    首先,利用到了国内牛人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();
            }

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

  • 相关阅读:
    天梯赛5-12 愿天下有情人都是失散多年的兄妹 【dfs】
    poj2718 Smallest Difference【贪心】
    HDU problem 5635 LCP Array【思维】
    codeforces 782C Andryusha and Colored Balloons【构造】
    HDU 4278 Faulty Odometer【进制转换】
    codeforces B. The Meeting Place Cannot Be Changed【二分】
    POJ 3264 Balanced Lineup 【线段树】
    HDU 1850
    CodeForces-714C
    HDU Problem 1247 Hat's Words 【字典树】
  • 原文地址:https://www.cnblogs.com/ithuo/p/4871167.html
Copyright © 2011-2022 走看看