首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从表Fizzler解析HTML

从表Fizzler解析HTML
EN

Stack Overflow用户
提问于 2014-03-20 13:50:51
回答 1查看 1.2K关注 0票数 1

我必须解析以下HTML页面:

这是我使用Fizzler进行解析的代码,我想得到的是标题、费率、天数(有时为空)和价格;在运行span.But后的第二个价格,它可以从ListRoomDetails中得到2个对象,如下所示,我们有房间类型10 %和房间类型2 60%,但是它跳过了Room类型2 60 %,得到了listRoomDetails的第一个元素(listRoomDetails类型1升级90%)。

我想把所有的房间类型都保留在两个ListRoomDetails div中

是否也有任何方法检测天值是否存在,如果存在,则得到它,否则,忽略它。

代码语言:javascript
复制
//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&euro;</span>
                        261.00&euro; //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&euro;</span>
                        261.00&euro; // 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&euro;</span>
                        261.00&euro;
                    </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&euro;</span>
                        261.00&euro;
                    </td>

                </tr>
            </tbody>
        </div>
   </div>

源代码:

代码语言:javascript
复制
        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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-20 15:19:41

您应该查询当前房间的详细信息(即ListItem):

代码语言:javascript
复制
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生成:

代码语言:javascript
复制
[
  {
     HotelName: "Super Room",
     Price: "290.00&euro;",
     TypeRooms: "Room Type 1 promotion 10%"
  },
  {
    HotelName: "Super Room",
    Price: "290.00&euro;",
    TypeRooms: "Room Type 2 promotion 60%"
  },
  {
    HotelName:  "Super Room",
    Price: "290.00&euro;",
    TypeRooms: "Room Type 1 promotion 90%"
  },
  {
    HotelName: "Super Room",
    Price: "290.00&euro;",
    TypeRooms: "Room Type 2 promotion 0 % // type of room"
  }
]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22535051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档