开发者

about the web parsing [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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.Le开发者_StackOverflow中文版ngth);
            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);

        }




    }

this is my code., it is given as assignment to me.. i have to separate the content between the tags ... and the links alone from a web page... i find it very difficult,. please help me out as soon as possible..


Parsing HTML is difficult and you should try using a 3rd party framework that builds a HTML DOM (preferably using some form of tokenizer) instead of using regular expressions. As you are using .NET then I would strongly suggest that you look at using the HTMLAgility Pack.

It (HTML Agility Pack) is a .NET code library that allows you to parse "out of the web" HTML files. The parser is very tolerant with "real world" malformed HTML. The object model is very similar to what proposes System.Xml, but for HTML documents (or streams).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜