现在的位置: 首页 > 综合 > 正文

这则获取内容

2014年01月27日 ⁄ 综合 ⁄ 共 785字 ⁄ 字号 评论关闭

    protected void Page_Load(object sender, EventArgs e)
    {
        string inputString = getHtml("http://www.sina.com.cn/");
        StringBuilder sb = new StringBuilder();
        Regex reg = null;
        Match mch = null;

        reg = new Regex(@"<a[^<>]*?hrefs*=s*['""s]([^""']*)['""][^<>]*?>(.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        for (mch = reg.Match(inputString); mch.Success; mch = mch.NextMatch())
        {
            sb.AppendLine("网站:" + mch.Groups[2] + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
            sb.AppendLine("地址:" + mch.Groups[1] + "</br>");

        }
        Response.Write(sb.ToString());
    }

    private string getHtml(string url)
    {
        WebClient myWebClient = new WebClient();
        byte[] myDataBuffer = myWebClient.DownloadData(url);
        return Encoding.Default.GetString(myDataBuffer);
    }

抱歉!评论已关闭.