HtmlAgilityPack
하여 img 요소의 너비와 높이를 가져 오는 방법이 작업은 다음과 같습니다.
HtmlAgilityPack.HtmlAttribute width = link.Attributes["width"];
HtmlAgilityPack.HtmlAttribute height = link.Attributes["height"];
그러나 너비와 높이는 대부분 null입니다. CSS의 높이와 너비를 얻는 방법?
이 페이지의베이스 : page
public sealed class UtilParserHTML
{
//Private Fields
private Uri Uri;
private Stream StreamPage;
private HttpWebRequest HttpRequest;
private HttpWebResponse HttpResponse;
//Public Fields
public HtmlDocument HtmlDocument { private set; get; }
public UtilParserHTML()
{
if (this.HtmlDocument == null)
HtmlDocument = new HtmlDocument();
}
public void LoadHTMLPage(string UrlPage)
{
if (string.IsNullOrEmpty(UrlPage))
throw new ArgumentNullException("");
CookieContainer cookieContainer = new CookieContainer();
this.Uri = new Uri(UrlPage);
this.HttpRequest = (HttpWebRequest)WebRequest.Create(UrlPage);
this.HttpRequest.Method = WebRequestMethods.Http.Get;
this.HttpRequest.CookieContainer = cookieContainer;
this.HttpResponse = (HttpWebResponse)this.HttpRequest.GetResponse();
this.StreamPage = this.HttpResponse.GetResponseStream();
this.HtmlDocument.Load(StreamPage);
}
public void LoadHTMLPage(FileStream StreamPage)
{
if (StreamPage == null)
throw new ArgumentNullException("");
HtmlDocument.Load(StreamPage);
}
public HtmlNodeCollection GetNodesByExpression(string XPathExpression)
{
if (string.IsNullOrEmpty(XPathExpression))
throw new ArgumentNullException("");
return this.HtmlDocument.DocumentNode.SelectNodes(XPathExpression);
}
XPath 를 사용하여 html로 이동합니다. 이 경우에는이 Xpath 식을 사용했습니다. // div [@ class = 'arrowRibbon'] // img
봐봐 ...
this.ParserHTML.LoadHTMLPage("http://www.img.com.br/default.aspx");
HtmlNodeCollection HtmlNodeCollectionResult = this.ParserHTML.GetNodesByExpression(Page.XPathExpression);
if (HtmlNodeCollectionResult != null)
{
foreach (HtmlNode NodeResult in HtmlNodeCollectionResult)
{
var src = NodeResult.Attributes["src"].Value;
}
}
...
편집하다
너비와 높이를 가진이 완벽한 예제를 보라.
this.ParserHTML.LoadHTMLPage("http://www.w3schools.com/tags/tag_img.asp");
HtmlNodeCollection HtmlNodeCollectionResult = this.ParserHTML.GetNodesByExpression("//div[@class='tryit_ex'] //img");
if (HtmlNodeCollectionResult != null)
{
foreach (HtmlNode NodeResult in HtmlNodeCollectionResult)
{
var src = NodeResult.Attributes["src"].Value;
var w = NodeResult.Attributes["width"].Value;
var h = NodeResult.Attributes["height"].Value;
}
}
희망이 도움이됩니다.