C# 来自表Fizzler的HTML解析
我必须解析以下HTML页面: 这是我使用Fizzler解析的代码,我想得到的是标题、费率、天数(有时为空)和价格;span之后的第二个价格。但当我运行代码时,它只能从ListRoomDetails中获得2个对象,如下所示,我们有房间类型1促销10%和房间类型2 60%,但它跳过了房间类型2 60%并获得了ListRoomDetails的第一个元素(房间类型1促销90%) 我希望将所有房间类型保留在两个ListRoomDetails分区中 是否还有任何方法可以检测days值是否存在,如果存在,则获取它,否则忽略它C# 来自表Fizzler的HTML解析,c#,html,html-agility-pack,fizzler,C#,Html,Html Agility Pack,Fizzler,我必须解析以下HTML页面: 这是我使用Fizzler解析的代码,我想得到的是标题、费率、天数(有时为空)和价格;span之后的第二个价格。但当我运行代码时,它只能从ListRoomDetails中获得2个对象,如下所示,我们有房间类型1促销10%和房间类型2 60%,但它跳过了房间类型2 60%并获得了ListRoomDetails的第一个元素(房间类型1促销90%) 我希望将所有房间类型保留在两个ListRoomDetails分区中 是否还有任何方法可以检测days值是否存在,如果存在,则获
//HTML File
<div class="ListItem">
<div class="ListRoom">
<span class="title">
<strong>Super Room</strong>
</span>
</div>
//section to get details of room
<div class="listRoomDetails">
<table>
<thead>
<tr>
Days
</tr>
</thead>
<tbody>
<tr>
<td class = "rates">
Room Type 1 promotion 10%
</td>
<td class = "days">
261.00
</td>
<td class = "days">
</td>
<td class="price">
<span>290.00€</span>
261.00€ //get this money
</td>
</tr>
<tr>
<td class = "rates">
Room Type 2 promotion 60%
</td>
<td class = "days">
</td>
<td class = "days">
261.00
</td>
<td class="price">
<span>290.00€</span>
261.00€ // get this money
</td>
</tr>
</tbody>
</div>
<div class="listRoomDetails">
<table>
<thead>
<tr>
Days
</tr>
</thead>
<tbody>
<tr>
<td class = "rates">
Room Type 1 promotion 90%
</td>
<td class = "days">
</td>
<td class = "rates">
261.00
</td>
<td class="price">
<span>290.00€</span>
261.00€
</td>
</tr>
<tr>
<td class = "rates">
Room Type 2 promotion 0 % // type of room
</td>
<td class = "days">
261.00
</td>
<td class="price">
<span>290.00€</span>
261.00€
</td>
</tr>
</tbody>
</div>
</div>
您应该查询当前房间的房间详细信息(即ListItem): 对于您的示例html,它将生成:
[
{
HotelName: "Super Room",
Price: "290.00€",
TypeRooms: "Room Type 1 promotion 10%"
},
{
HotelName: "Super Room",
Price: "290.00€",
TypeRooms: "Room Type 2 promotion 60%"
},
{
HotelName: "Super Room",
Price: "290.00€",
TypeRooms: "Room Type 1 promotion 90%"
},
{
HotelName: "Super Room",
Price: "290.00€",
TypeRooms: "Room Type 2 promotion 0 % // type of room"
}
]
代码在哪里?html在哪里?@SergeyBerezovskiy:我已经添加了一个选项,请您检查一下,谢谢。我在您的列表中没有看到带有
tableItem
类的元素html@SergeyBerezovskiy:感谢您指出它实际上位于顶部,名称为ListItem,我已更改以测试代码,但忘记更改代码。非常感谢您的指导,但是我在.price span中得到了一个Null错误,实际上我的想法是让价格超出标签,在本例中是261.00欧元。当我尝试时。价格只有当它同时获得两个价格时,我可以尝试其他方法/Thanks@bluewonder您能否给出一个示例行,该行为Null提供错误信息?@bluewonder要获取超出范围的价格,您可以使用rd.QuerySelector(“.price:last child”).InnerText.Trim()
是的,我尝试了这两种情况,它在这里有一个空错误:Price=rd.QuerySelector(“.Price span”).InnerText.Trim()和.Price:last child。我也在再次检查,谢谢你@SergeyBerezovskiy@bluewonder您能否给出一个示例行,该行为Null提供错误信息?
var rooms = from r in doc.QuerySelectorAll(".ListItem")
from rd in r.QuerySelectorAll(".listRoomDetails tbody tr")
select new HotelAvailability {
HotelName = r.QuerySelector(".title").InnerText.Trim(),
TypeRooms = rd.QuerySelector(".rates").InnerText.Trim(),
Price = rd.QuerySelector(".price span").InnerText.Trim()
};
[
{
HotelName: "Super Room",
Price: "290.00€",
TypeRooms: "Room Type 1 promotion 10%"
},
{
HotelName: "Super Room",
Price: "290.00€",
TypeRooms: "Room Type 2 promotion 60%"
},
{
HotelName: "Super Room",
Price: "290.00€",
TypeRooms: "Room Type 1 promotion 90%"
},
{
HotelName: "Super Room",
Price: "290.00€",
TypeRooms: "Room Type 2 promotion 0 % // type of room"
}
]