【问题标题】:about the web parsing [closed]关于网络解析[关闭]
【发布时间】:2010-01-18 10:05:16
【问题描述】:

private void button3_Click(object sender, EventArgs e) { listBox1.Items.Clear();

        string szURL = textBox1.Text;// "http://localhost";
        //textBox1.Text = szURL;
        HttpWebRequest httpRequest;
        HttpWebResponse httpResponse;
        string bodyText = "";
        Stream responseStream;
        Byte[] RecvBytes = new Byte[Byte.MaxValue];
        Int32 bytes;
        httpRequest = (HttpWebRequest)WebRequest.Create(szURL);
        httpResponse = (HttpWebResponse)httpRequest.GetResponse();
        responseStream = httpResponse.GetResponseStream();
        while (true)
        {
            bytes = responseStream.Read(RecvBytes,
            0, RecvBytes.Length);
            if (bytes <= 0) break;
            bodyText += System.Text.Encoding.UTF8.GetString(RecvBytes,
            0, bytes);
        }
        //listBox1.Items.Add( bodyText);
        textBox2.Text = bodyText;

        MatchCollection m1 = Regex.Matches(bodyText, @"(<a.*?>.*?</a>)", 
              RegexOptions.Singleline);

        // 2.
        // Loop over each match.
        foreach (Match m in m1)
        {
            string value = m.Groups[1].Value;
            //   LinkItem i = new LinkItem();

            // 3.
            // Get href attribute.
            Match m2 = Regex.Match(value, @"<\s*script[^>]*>(?<content>.*?)<\s*/\s*\script\s*>",
                RegexOptions.Singleline);
            if (m2.Success)
            {
                listBox1.Text = m2.Groups[1].Value;
            }

            // 4.
            // Remove inner tags from text.

            string t = Regex.Replace(value, @"\s*<.*?>\s*", "",
                RegexOptions.Singleline);
            // i.Text = t;
            listBox1.Items.Clear();
            listBox1.Items.Add(t);

        }




    }

这是我的代码。它是作为分配给我的。我必须将标签之间的内容分开......以及单独从网页中的链接......我觉得这很困难,。请尽快帮助我..

【问题讨论】:

    标签: c#


    【解决方案1】:

    解析 HTML 很困难,您应该尝试使用构建 HTML DOM 的 3rd 方框架(最好使用某种形式的标记器)而不是使用正则表达式。当您使用 .NET 时,我强烈建议您考虑使用 HTMLAgility Pack

    It (HTML Agility Pack) 是一个 .NET 代码库,可让您解析“网络外”的 HTML 文件。解析器对“真实世界”格式错误的 HTML 非常宽容。对象模型与 System.Xml 的提议非常相似,但用于 HTML 文档(或流)。

    【讨论】:

      猜你喜欢
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 2017-04-04
      • 2012-02-28
      相关资源
      最近更新 更多