Разбор HTML со стола Fizzler
Я должен разобрать следующую страницу HTML:
Это мой код парсинга с использованием Fizzler, я хочу получить название, цены, дни (иногда нулевые) и цену; вторая цена после span. Но когда я запускаю свой код, он просто может получить 2 объекта из ListRoomDetails, как показано ниже, у нас есть промоушен Тип комнаты 1 10 % и Тип комнаты 2 60%, но он пропустил Тип комнаты 2 60% и получить первый элемент listRoomDetails (промоция Тип комнаты 1 90%).
Я хочу сохранить все типы комнат в двух элементах ListRoomDetails
Также есть ли способ определить, существует ли значение дней, если оно существует, получить его, в противном случае игнорировать его.
//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>
Исходный код:
var source = File.ReadAllText("TestHtml/HotelWithAvailability.html");
var html = new HtmlDocument(); // with HTML Agility pack
html.LoadHtml(source);
var doc = html.DocumentNode;
var rooms = (from listR in doc.QuerySelectorAll(".ListItem")
from listR2 in doc.QuerySelectorAll("tbody")
select new HotelAvailability
{
HotelName = listR.QuerySelector(".title").InnerText.Trim(), //get room name
TypeRooms = listR2.QuerySelector("tr td.rates").InnerText.Trim(), //get room type
Price = listR2.QuerySelector("tr td.price").InnerText.Trim(), //
}).ToArray();
1 ответ
Вы должны запросить информацию о комнате текущей комнаты (т.е. ListItem):
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()
};
Для вашего образца 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"
}
]