解析HTML与CSQuery [英] Parsing HTML with CSQuery
本文介绍了解析HTML与CSQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何通过使用CSQuery的ID检索从 DIV
标签的价值?
How can I retrieve the value from a div
tag via the ID using CSQuery?
例如,
<h3>
<div id='type'>
Room 1
</div>
<div id='price'>
145
</div>
</h3>
在这种情况下,我想获得里面的内容键入
和价格
。
In this case I'd like to get the content inside type
and price
.
推荐答案
好吧,这里是你如何与一个完整的工作的例子做到这一点。
Ok, here is how you do this with a full working example.
HTML
这包括您对
var html = @"<h3>
<div id='lib_presta'>
Chambre standard 1 pers du <span class=''>03/03/2014</span> au <span class=''>05/03/2014 </span>
</div>
<div id='prix_presta'>
127.76 €
</div>
</h3><h3>
<div id='lib_presta'>
Chambre standard 2 pers du <span class=''>03/03/2014</span> au <span class=''>05/03/2014 </span>
</div>
<div id='prix_presta'>
227.76 €
</div>
</h3>";
C#code
这将加载DOM元素通过相应的ID为描述和价格的两个列表。
然后,它们投影到 HotelAvailability
的列表中使用两个集合为 HotelName
和<$ C的键值对象$ C>价格属性。
This loads the dom elements by their id's into two lists of descriptions and prices.
It then projects them into a list of HotelAvailability
objects using the key values of both collections as the HotelName
and Price
properties.
CQ dom = html;
var libs = dom["#lib_presta"];
var prixs = dom["#prix_presta"];
var list = libs.Zip(prixs, (k, v) => new { k, v })
.Select(h => new HotelAvailablity { HotelName = h.k.InnerText.Trim(), Price = h.v.InnerText.Trim() });
运行上面的控制台应用程序来测试它。
Run the above in a console app to test it.
这篇关于解析HTML与CSQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文