Parsing HTML with CSQuery

前端 未结 1 911
梦谈多话
梦谈多话 2020-12-10 17:45

How can I retrieve the value from a div tag via the ID using CSQuery?

For example,

Room 1

1条回答
  •  盖世英雄少女心
    2020-12-10 18:20

    Ok, here is how you do this with a full working example.

    Html

    This includes your invalid/duplicate id html which you have no control over

    var html = @"

    Chambre standard 1 pers du 03/03/2014 au 05/03/2014
    127.76 €

    Chambre standard 2 pers du 03/03/2014 au 05/03/2014
    227.76 €

    ";

    C# Code

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

    Screen grab

    Run the above in a console app to test it.

    0 讨论(0)
提交回复
热议问题