2010-01-18 6 views
0

개인 무효 button3_Click (개체 발신자, EventArgs 전자) { 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); 

     } 




    } 

이 내 코드입니다., 그것은 나에게 할당로 제공됩니다 .. 내가 태그 사이의 내용을 분리해야 ... 그리고 웹 페이지에서 혼자 링크 ... 나는 그것이 매우 어려운 찾을 수 ,. 최대한 빨리 도와주세요.

답변

1

HTML 구문 분석이 어려우므로 정규 표현식을 사용하는 대신 HTML DOM을 작성하는 타사 프레임 워크 (가능한 경우 토큰 화 프로그램의 일부 양식 사용)를 사용해보십시오. .NET을 사용할 때 HTMLAgility Pack을 사용하는 것이 좋습니다.

It (HTML Agility Pack)은 "웹 외부"HTML 파일을 구문 분석 할 수있는 .NET 코드 라이브러리입니다. 파서는 "실제"형식이 잘못된 HTML에 매우 관대합니다. 개체 모델은 System.Xml을 제안하는 것과 매우 유사하지만 HTML 문서 (또는 스트림)를 대상으로합니다.

+0

감사합니다. – user247958

관련 문제