• C# 抓取网页Html源码 (网络爬虫)


    http://www.cnblogs.com/wxxian001/archive/2011/09/07/2169519.html

    刚刚完成一个简单的网络爬虫,因为在做的时候在网上像无头苍蝇一样找资料。发现了很多的资料,不过真正能达到我需要,有用的资料--代码很难找。所以我想发这篇文章让一些要做这个功能的朋友少走一些弯路。

    首先是抓取Html源码,并选择<ul class="post_list">  </ul>节点的href:要添加 using System.IO;using System.Net;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    private void Search(string url)
    {
        string rl;
        WebRequest Request = WebRequest.Create(url.Trim());
     
        WebResponse Response = Request.GetResponse();
     
        Stream resStream = Response.GetResponseStream();
     
        StreamReader sr = new StreamReader(resStream, Encoding.Default);
        StringBuilder sb = new StringBuilder();
        while ((rl = sr.ReadLine()) != null)
        {
            sb.Append(rl);
        }
     
     
        string str = sb.ToString().ToLower();
     
        string str_get = mid(str, "<ul class="post_list">""</ul>");
     
     
        int start = 0;
        while (true)
        {
            if (str_get == null)
                break;
            string strResult = mid(str_get, "href="""""out start);
            if (strResult == null)
                break;
            else
            {
                lab[url] += strResult;
                str_get = str_get.Substring(start);
            }
        }
    }
     
     
     
     
    private string mid(string istr, string startString, string endString)
    {
        int iBodyStart = istr.IndexOf(startString, 0);               //开始位置
        if (iBodyStart == -1)
            return null;
        iBodyStart += startString.Length;                           //第一次字符位置起的长度
        int iBodyEnd = istr.IndexOf(endString, iBodyStart);         //第二次字符在第一次字符位置起的首次位置
        if (iBodyEnd == -1)
            return null;
        iBodyEnd += endString.Length;                              //第二次字符位置起的长度
        string strResult = istr.Substring(iBodyStart, iBodyEnd - iBodyStart - 1);
        return strResult;
    }
     
     
    private string mid(string istr, string startString, string endString, out int iBodyEnd)
    {
        //初始化out参数,否则不能return
        iBodyEnd = 0;
     
        int iBodyStart = istr.IndexOf(startString, 0);               //开始位置
        if (iBodyStart == -1)
            return null;
        iBodyStart += startString.Length;                           //第一次字符位置起的长度
        iBodyEnd = istr.IndexOf(endString, iBodyStart);         //第二次字符在第一次字符位置起的首次位置
        if (iBodyEnd == -1)
            return null;
        iBodyEnd += endString.Length;                              //第二次字符位置起的长度
        string strResult = istr.Substring(iBodyStart, iBodyEnd - iBodyStart - 1);
        return strResult;
    }

    好了,上面就是全部代码了,如果你想要运行出来的话,有些细节要自己修改下。

  • 相关阅读:
    学习 TList 类的实现[1]
    Dll 使用 PChar 参数的小例子
    学习使用资源文件[11]
    【转载】OGRE 内存管理
    【转载】Ogre的内存分配策略
    【转载】.NET面试题系列[0]
    【转载】深入探讨透视投影坐标变换
    【转载】四元数-Quaterion
    【转载】齐次坐标
    【转载】深入研究Windows内部原理绝对经典的资料
  • 原文地址:https://www.cnblogs.com/zkwarrior/p/5421107.html
Copyright © 2020-2023  润新知