2012-01-05 3 views
1

웹 페이지를 크롤링 할 수있는 ASP.NET 페이지를 작성하고 해당 URL이 포함되도록 편집 한 모든 관련 html 요소를 올바르게 표시하려고합니다.ASP.NET 웹 페이지 미러, 절대 경로로 모든 상대 URL 바꾸기

이 질문은 부분적으로 위의 대답의 조합 나는 다음을 구축 http://blog.abodit.com/2010/03/a-simple-web-crawler-in-c-using-htmlagilitypack/이 블로그 포스트를 사용하여 여기 https://stackoverflow.com/a/2719712/696638

대답하고있다;

public partial class Crawler : System.Web.UI.Page { 
    protected void Page_Load(object sender, EventArgs e) { 
     Response.Clear(); 

     string url = Request.QueryString["path"]; 

     WebClient client = new WebClient(); 
     byte[] requestHTML = client.DownloadData(url); 
     string sourceHTML = new UTF8Encoding().GetString(requestHTML); 

     HtmlDocument htmlDoc = new HtmlDocument(); 
     htmlDoc.LoadHtml(sourceHTML); 

     foreach (HtmlNode link in htmlDoc.DocumentNode.SelectNodes("//a[@href]")) { 
      if (!string.IsNullOrEmpty(link.Attributes["href"].Value)) { 
       HtmlAttribute att = link.Attributes["href"]; 
       string href = att.Value; 

       // ignore javascript on buttons using a tags 
       if (href.StartsWith("javascript", StringComparison.InvariantCultureIgnoreCase)) continue; 

       Uri urlNext = new Uri(href, UriKind.RelativeOrAbsolute); 
       if (!urlNext.IsAbsoluteUri) { 
        urlNext = new Uri(new Uri(url), urlNext); 
        att.Value = urlNext.ToString(); 
       } 
      } 
     } 

     Response.Write(htmlDoc.DocumentNode.OuterHtml); 

    } 
} 

이것은 링크의 href 속성을 대체합니다. 이것을 확장함으로써 가장 효율적인 방법이 무엇을 포함해야하는지 알고 싶습니다. <a> 요소위한

  • href 속성
  • <link>
  • 요소에 대한 속성 href
  • <script>
  • 요소에 대한 속성 src
  • <img> 요소
  • action 속성 <form> 요소에 대한 속성 src

사람들이 생각할 수있는 다른 것들은 무엇입니까?

몬스터 xpath와 함께 SelectNodes에 대한 단일 호출을 사용하여이 값을 찾을 수 있습니까? 아니면 SelectNodes를 여러 번 호출하여 각 컬렉션을 반복하는 것이 더 효율적입니까?

SelectNodes("//*[@href or @src or @action]") 

을 한 다음 아래의 if 문을 적응해야 할 것 :

답변

3

다음은 작동합니다.

+0

감사합니다. SelectNodes ("// * [@ href or @src or @action]")'로 변경해야만 아무 것도 선택할 수 없었습니다. 이것이 가장 효율적인 솔루션입니까? –

+0

죄송합니다, 그게 내가 의미하는 것입니다. 효율성은 문서의 크기 및 구조와 같은 특정 요소에 따라 달라집니다. 링크가없는 문서의 특정 섹션이 있다는 것을 안다면 xpath로 작업하거나 xpath를 작은 쿼리로 나눌 수도 있습니다. – Digbyswift

관련 문제