我嘗試使用HtmlAgilityPack從html解析圖像URL。在html文件中我有img標籤:使用HtmlAgilityPack-Xpath解析HTML文檔,RegExp
<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標籤屬性src。我需要這個:http://213.215.107.125/fotky/1358/93/v_13589304.jpg?v=6。
我知道這一點:
- Src的屬性附加傷害包括URL,URL開始 與 http://213.215.107.125/fotky
- 我知道ALT屬性附加傷害網址的價值 有 變量lenght和html doc 包括其他的帶有url的標籤,它以 開頭http://213.215.107.125/fotky
- 我知道img標籤的alt屬性(Fotka:Myke015))
任何進展,我嘗試了很多方法,但是沒有什麼工作好。
最後我試試這個:
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);
}
}
}
Oded,我該如何在Xpath中指定v arc的src atribute。 src =「Fotka:miky015」 – Tom 2010-11-02 12:54:20
@Lucas - 此時,您可以使用'node.InnerText'來獲取值。 – Oded 2010-11-02 12:55:18
Oded我用你的advace,但是我得到錯誤:'// img @ src'有一個無效的標記,如果我用你的Xpath exp「// img @ src」。 – Tom 2010-11-02 13:00:35