Parse html document using HtmlAgilityPack

青春壹個敷衍的年華 提交于 2019-12-02 09:44:15

问题


I'm trying to parse the following html snippet via HtmlAgilityPack:

<td bgcolor="silver" width="50%" valign="top">
 <table bgcolor="silver" style="font-size: 90%" border="0" cellpadding="2" cellspacing="0"
                                                width="100%">
   <tr bgcolor="#003366">
       <td>
           <font color="white">Info
        </td>
        <td>
           <font color="white">
              <center>Price
                   </td>
                      <td align="right">
                         <font color="white">Hourly
                         </td>
              </tr>
               <tr>
                 <td>
                     <a href='test1.cgi?type=1'>Bookbags</a>
                 </td>
                   <td>
                      $156.42
                    </td>
                    <td align="right">
                        <font color="green">0.11%</font>
                      </td>
                  </tr>
                  <tr>
                    <td>
                       <a href='test2.cgi?type=2'>Jeans</a>
                     </td>
                         <td>
                            $235.92
                               </td>
                                  <td align="right">
                                     <font color="red">100%</font>
                                  </td>
                   </tr>
               </table>
          </td>

My code looks something like this:

private void ParseHtml(HtmlDocument htmlDoc)
{
    var ItemsAndPrices = new Dictionary<string, int>();
   var findItemPrices = from links in htmlDoc.DocumentNode.Descendants()
                             where links.Name.Equals("table") && 
                             links.Attributes["width"].Equals ("100%") && 
                             links.Attributes["bgcolor"].Equals("silver")
                            select new
                                       {
                                           //select item and price
                                       }

In this instance, I would like to select the item which are Jeans and Bookbags as well as their associated prices below and store them in a dictionary.

E.g Jeans at price $235.92

Does anyone know how to do this properly via htmlagility pack and LINQ?


回答1:


Here's what I came up with:

        var ItemsAndPrices = new Dictionary<string, string>();
        var findItemPrices = from links in htmlDoc.DocumentNode.Descendants("tr").Skip(1)
                             select links;

        foreach (var a in findItemPrices)
        {
            var values = (from tds in a.Descendants("td")
                         select tds.InnerText.Trim()).ToList();

            ItemsAndPrices.Add(values[0], values[1]);
        }

The only thing I changed was your <string, int>, because $156.42 isn't an int




回答2:


Try this: Regex solution:

  static Dictionary<string, string> GetProduct(string name, string html)
    {
        Dictionary<string, string> output = new Dictionary<string, string>();
        string clfr = @"[\r\n]*[^\r\n]+";
        string pattern = string.Format(@"href='([^']+)'>{0}</a>.*{1}{1}[\r\n]*([^\$][^\r\n]+)", name, clfr);
        Match products = Regex.Match(html, pattern, RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
        if(products.Success) {
            GroupCollection details = products.Groups;
            output.Add("Name", name);
            output.Add("Link", details[1].Value);
            output.Add("Price", details[2].Value.Trim());
            return output;
        }
        return output;
    }

Then:

 var ProductNames = new string[2] { "Jeans", "Bookbags" };
    for (int i = 0, len = ProductNames.Length; i < len; i++)
    {
        var product = GetProduct(ProductNames[i], html);
          if (product.Count != 0)
          {
            Console.WriteLine("{0} at price {1}", product["Name"], product["Price"]);
          }
    }

Output:

Jeans at price $235.92
Bookbags at price $156.42

Note: The value of Dictionary can't be an int because $235.92/$156.42 is not an valid int. to transform it to an int valid, you can remove the dollar and dot symbol and use

int.Parse()



回答3:


Assuming that there could be other rows and you don't specifically want only Bookbags and Jeans, I'd do it like this:

var table = htmlDoc.DocumentNode
    .SelectSingleNode("//table[@bgcolor='silver' and @width='100%']");
var query =
    from row in table.Elements("tr").Skip(1) // skip the header row
    let columns = row.Elements("td").Take(2) // take only the first two columns
        .Select(col => col.InnerText.Trim())
        .ToList()
    select new
    {
        Info = columns[0],
        Price = Decimal.Parse(columns[1], NumberStyles.Currency),
    };


来源:https://stackoverflow.com/questions/7758792/parse-html-document-using-htmlagilitypack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!