문제

I want to catch data from a website useing HtmlAgilityPack. I use this code to get src but it returns null.

Html code:

<div style="margin: auto; text-align: center;">
    <img src="index?option=com_capcha&task=capcha" />

C# code:

HtmlNodeCollection nodes1 = doc.DocumentNode.SelectNodes("//div[@style='margin: auto; text-align: center;']/img[@src]");
도움이 되었습니까?

해결책

You have to select the node first, then get the attribute. Do this by using:

HtmlNode.GetAttributeValue(attribute,default);

it will return attribute value or a default if the attribute is not present.

This will get you the src attribute for each of the nodes in your node collection:

foreach(HtmlNode node in nodes1){
    string srcValue = node.GetAttributeValue("src","none");
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top