【发布时间】:2010-11-02 12:29:35
【问题描述】:
我尝试使用 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: 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 属性 Url 的值 有 可变长度和 html doc 由其他带有 url 的 img 标签组成,以 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);
}
}
}
【问题讨论】: