나는 HtmlAgilitiPack
배우고 HtmlAgilitiPack
, 가치를 얻는 법을 묻고 싶다. ( 나는이 가치를 원한다 ) :
HTML 페이지에서 :
<div id="js_citySelectContainer" class="select_container city_select shorten_text replaced">
<span class="dropDownButton ownCity coords">
<a>i want get this value</a>
</span>
</div>
C # 코드 :
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
Console.writeln(echo i want get this value);
나는 시도했다 :
doc.DocumentNode.Descendants("span").Where(s => s.GetAttributeValue("class", "") == "dropDownButton ownCity coords").First().InnerText;
하지만 그것은 나에게 도움이 될 수 있습니까? 고맙습니다.
XPATH 구문을 사용할 수 있습니다.
var span = doc.DocumentNode.SelectSingleNode("//span[@class='dropDownButton ownCity coords']");
var anchorText = span.ChildNodes["a"].InnerText;
LINQ를 사용할 수도 있습니다.
var anchorTexts =
from span in doc.DocumentNode.Descendants("span")
where span.GetAttributeValue("class", "") == "dropDownButton ownCity coords"
from anchor in span.Descendants("a")
select anchor.InnerText;
string anchorText = anchorTexts.FirstOrDefault();
나는 당신이 span
텍스트를 얻으려고하고 있다고 생각한다 a
텍스트가 필요하다.
이 시도
doc.DocumentNode.Descendants("span")
.Where(s => s.GetAttributeValue("class", "") == "dropDownButton ownCity coords")
.First().Descendants("a").First().InnerText;