about the web parsing
专用的void按钮3_click(对象发送器,事件参数e){listbox 1.items.clear();
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 | 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, @"(.*?)", 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); } } |
这是我的代码,它是作为分配给我的。我必须把标签之间的内容分开…只有网页上的链接…我觉得很难。请尽快帮助我。
解析HTML很困难,您应该尝试使用构建HTML DOM的第三方框架(最好使用某种形式的标记器),而不是使用正则表达式。当您使用.NET时,我强烈建议您考虑使用HTMLAgility包。
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).
号