【问题标题】:HTML Parsing from the table Fizzler从表格 Fizzler 解析 HTML
【发布时间】:2014-03-20 23:23:40
【问题描述】:

我必须解析以下 HTML 页面:

这是我使用 Fizzler 解析的代码,我想要得到的是标题、费率、天数(有时为空)和价格; span 之后的第二个价格。但是当我运行我的代码时,它只能从 ListRoomDetails 中获取 2 个对象,如下所示,我们有 Room Type 1 促销 10 % 和 Room type 2 60%,但它跳过了 Room type 2 60 % 和获取 listRoomDetails 的第一个元素(房间类型 1 提升 90%)。

我希望将所有房间类型保留在两个 ListRoomDetails div 中

还有没有办法检测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&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>

源代码:

        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();

【问题讨论】:

  • 代码在哪里? html在哪里?
  • @SergeyBerezovskiy:我已经添加了,请您检查一下谢谢
  • 我在你的 html 中没有看到带有 tableItem 类的元素
  • @SergeyBerezovskiy:感谢您指出实际上它位于顶部,名称为 ListItem,我已更改以测试代码并忘记将其改回

标签: c# html html-agility-pack fizzler


【解决方案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&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"
  }
]

【讨论】:

  • 非常感谢您的指导,但我在 .price 跨度处出现 Null 错误,实际上我的想法是在 标签之外获取价格,在本例中为 261.00 &欧元。当我尝试 .price 时,它​​会同时获得 2 个价格,我可以尝试任何其他方式/谢谢
  • @bluewonder 你能给出一个示例行,它会给你 Null 错误吗?
  • @bluewonder 获取超出范围的价格,您可以使用 rd.QuerySelector(".price:last-child").InnerText.Trim()
  • 是的,我尝试了这两种情况,这里有一个 NULL 错误:Price = rd.QuerySelector(".price span").InnerText.Trim() 还有 .price:last-child。我也在再次检查,谢谢@Sergey Berezovskiy
  • @bluewonder 你能给出一个示例行,它会给你 Null 错误吗?
猜你喜欢
  • 2013-01-02
  • 2011-01-04
  • 2013-04-09
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-20
  • 2014-09-03
相关资源
最近更新 更多