使用htmlagilitypackxpath、RegExp解析HTML文档
我尝试用HtmlAgilityPack从html解析图像url。在html文档中,我有img标记:使用htmlagilitypackxpath、RegExp解析HTML文档,xpath,html-agility-pack,Xpath,Html Agility Pack,我尝试用HtmlAgilityPack从html解析图像url。在html文档中,我有img标记: <a class="css_foto" href="" title="Fotka: MyKe015"> <span> <img src="http://213.215.107.125/fotky/1358/93/v_13589304.jpg?v=6" width="176" height="216" alt="Fotka: M
<a class="css_foto" href="" title="Fotka: MyKe015">
<span>
<img src="http://213.215.107.125/fotky/1358/93/v_13589304.jpg?v=6"
width="176" height="216" alt="Fotka: MyKe015" />
</span>
</a>
我需要从这个img标签atribute src获取。我需要这个:http://213.215.107.125/fotky/1358/93/v_13589304.jpg?v=6.
我知道这一点:
List<string> src;
var req = (HttpWebRequest)WebRequest.Create("http://pokec.azet.sk/myke015");
req.Method = "GET";
using (WebResponse odpoved = req.GetResponse())
{
var htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.Load(odpoved.GetResponseStream());
var nodes = htmlDoc.DocumentNode.SelectNodes("//img[@src]");
src = new List<string>(nodes.Count);
if (nodes != null)
{
foreach (var node in nodes)
{
if (node.Id != null)
src.Add(node.Id);
}
}
}
列表src;
var req=(HttpWebRequest)WebRequest.Create(“http://pokec.azet.sk/myke015");
req.Method=“GET”;
使用(WebResponse odpoved=req.GetResponse())
{
var htmlDoc=新的HtmlAgilityPack.HtmlDocument();
Load(odpoved.GetResponseStream());
var nodes=htmlDoc.DocumentNode.SelectNodes(“//img[@src]”);
src=新列表(nodes.Count);
如果(节点!=null)
{
foreach(节点中的var节点)
{
如果(node.Id!=null)
src.Add(node.Id);
}
}
}
您的XPath选择img
节点,而不是属于它们的src
属性
而不是(选择具有src
属性的所有图像标记):
使用此选项(选择作为所有img
元素的子节点的src
属性):
此XPath 1.0表达式:
//a[@alt='Fotka: MyKe015']/@src
Oded,如何在Xpath中指定src atribute的值。src=“Fotka:miky015”@Lucas-此时,您可以使用
node.InnerText
获取值。Oded我使用了您的advace,但我得到了错误:'//img@src'的令牌无效,如果我使用了您的Xpath exp'//img@src“@Lucas-抱歉,我错过了一个/
。它应该是//img/@src
。
var nodes = htmlDoc.DocumentNode.SelectNodes("//img/@src");
//a[@alt='Fotka: MyKe015']/@src