htmlagilitypack을 처음 접했을 때 HTML과 같은 링크를 얻을 수있는 방법을 알아 내려고 노력했습니다.
<div class="std"><div style="border-right: 1px solid #CCCCCC; float: left; height: 590px; width: 190px;"><div style="background-color: #eae3db; padding: 8px 0 8px 20px; font-weight: bold; font-size: 13px;">test</div>
<div>
<div style="font-weight: bold; margin: 5px 0 -6px;">FEATURED</div>
<span class="widget widget-category-link"><a href="http://www.href1.com"><span>cat1</span></a></span>
<span class="widget widget-category-link"><a href="http://www.href1.com"><span>cat2</span></a></span>
</div></div>
아직 C #에서는 코드를 작성하지 않았지만 HTML ID가 없을 때 링크와 내부 텍스트를 가져 오기 위해 어떤 태그가 가리켜 야하는지 다른 사람이 조언 할 수 있는지 궁금합니다. ' 감사
XPATH 에 익숙하다면 html의 요소와 속성을 탐색하여 원하는 것을 얻을 수 있습니다. 위의 각 href를 얻으려면 다음과 같이 코드를 작성할 수 있습니다.
const string xpath = "/div//span/a";
//WebPage below is a string that contains the text of your example
HtmlNode html = HtmlNode.CreateNode(WebPage);
//The following gives you a node collection of your two <a> elements
HtmlNodeCollection items = html.SelectNodes(xpath);
foreach (HtmlNode a in items)
{
if (a.Attributes.Contains("href"))
//Get your value here
{
yourValue = a.Attributes["href"].Value
}
}
참고 :이 코드를 실행하거나 테스트하지 않았습니다.