【问题标题】:HtmlAgilityPack Scrape table from websiteHtmlAgilityPack 从网站上抓取表格
【发布时间】:2022-07-10 21:21:17
【问题描述】:

我正在学习如何使用 HtmlAgilityPack,但我找不到任何有关按我需要的方式抓取表格的文档。

我的桌子是这样的

|     | NAME                                  | PRICE            | TIME      |
| --- | ------------------------------------- | ---------------- | --------- |
|     | Arma 3                                | $29.99           | 1586h 57m |
|     | DayZ                                  | $44.99           | 28h 05m   |
|     | Survarium                             | Free or No Price | 02h 25m   |
|     | Squad                                 | $49.99           | 11h 05m   |
|     | Squad - Public Testing                | Not in store     | 0h 0m     |
|     | Counter-Strike: Global Offensive      | Free or No Price | 00h 26m   |
|     | Infestation: Survivor Stories Classic | Free or No Price | 00h 05m   |
|     | PLAYERUNKNOWN'S BATTLEGROUNDS         | $29.99           | 0h 0m     |

我已经尝试了很多在 Google 上找到的关于表格和 HtmlAgilityPack 的东西,但都没有奏效。 我有一个列表视图,我希望数据进入。我唯一想要的就是 gnamegpricegtime 加载到列表视图并记住这些值会根据列出的游戏而变化。

我无法发布我已经尝试过的内容,因为有很多网站帖子需要返回并找到。

网站上表格的html是这样的

<div class="col-12">
                    <h2>Dan Andrews Steam Profile 8                        Games</h2>
                    <div class="table-responsive">
                        <table class="table game-table">
                            <tbody>
                            <tr>
                                <th></th>
                                <th class="gname">NAME</th>
                                <th style="width:200px">PRICE</th>
                                <th style="width:200px">TIME</th>
                            </tr>

                                                            <tr>
                                    <td class="gicon"><img src="https://cdn.cloudflare.steamstatic.com/steam/apps/107410/capsule_184x69.jpg" onerror="this.src='/assets/images/applogo.svg'">
                                    </td>
                                    <td class="gname">Arma 3</td>
                                    <td class="gprice">$29.99</td>
                                    <td class="gtime">1586h 57m</td>
                                </tr>
                                                            <tr>
                                    <td class="gicon"><img src="https://cdn.cloudflare.steamstatic.com/steam/apps/221100/capsule_184x69.jpg" onerror="this.src='/assets/images/applogo.svg'">
                                    </td>
                                    <td class="gname">DayZ</td>
                                    <td class="gprice">$44.99</td>
                                    <td class="gtime">28h 05m</td>
                                </tr>
                                                            <tr>
                                    <td class="gicon"><img src="https://cdn.cloudflare.steamstatic.com/steam/apps/355840/capsule_184x69.jpg" onerror="this.src='/assets/images/applogo.svg'">
                                    </td>
                                    <td class="gname">Survarium</td>
                                    <td class="gprice">Free or No Price</td>
                                    <td class="gtime">02h 25m</td>
                                </tr>
                                                            <tr>
                                    <td class="gicon"><img src="https://cdn.cloudflare.steamstatic.com/steam/apps/393380/capsule_184x69.jpg" onerror="this.src='/assets/images/applogo.svg'">
                                    </td>
                                    <td class="gname">Squad</td>
                                    <td class="gprice">$49.99</td>
                                    <td class="gtime">11h 05m</td>
                                </tr>
                                                            <tr>
                                    <td class="gicon"><img src="https://cdn.cloudflare.steamstatic.com/steam/apps/774941/capsule_184x69.jpg" onerror="this.src='/assets/images/applogo.svg'">
                                    </td>
                                    <td class="gname">Squad - Public Testing</td>
                                    <td class="gprice">Not in store</td>
                                    <td class="gtime">0h 0m</td>
                                </tr>
                                                            <tr>
                                    <td class="gicon"><img src="https://cdn.cloudflare.steamstatic.com/steam/apps/730/capsule_184x69.jpg" onerror="this.src='/assets/images/applogo.svg'">
                                    </td>
                                    <td class="gname">Counter-Strike: Global Offensive</td>
                                    <td class="gprice">Free or No Price</td>
                                    <td class="gtime">00h 26m</td>
                                </tr>
                                                            <tr>
                                    <td class="gicon"><img src="https://cdn.cloudflare.steamstatic.com/steam/apps/226700/capsule_184x69.jpg" onerror="this.src='/assets/images/applogo.svg'">
                                    </td>
                                    <td class="gname">Infestation: Survivor Stories Classic</td>
                                    <td class="gprice">Free or No Price</td>
                                    <td class="gtime">00h 05m</td>
                                </tr>
                                                            <tr>
                                    <td class="gicon"><img src="https://cdn.cloudflare.steamstatic.com/steam/apps/578080/capsule_184x69.jpg" onerror="this.src='/assets/images/applogo.svg'">
                                    </td>
                                    <td class="gname">PLAYERUNKNOWN'S BATTLEGROUNDS</td>
                                    <td class="gprice">$29.99</td>
                                    <td class="gtime">0h 0m</td>
                                </tr>
                                                        </tbody>
                        </table>
                    </div>
                </div>
    ```

【问题讨论】:

    标签: vb.net html-agility-pack


    【解决方案1】:

    这是 C# 中的一个可能的解决方案(即使问题是用 VB.Net 标记的)。您可以在此处获得 Game 对象的列表。然后您可以将它们绑定到 ListView 或您可能需要的任何东西。

        public class Game
        {
            public string Img { get; set; }
            public string Name { get; set; }
            public double? Price { get; set; }
            public string? PriceStatus { get; set; }
            public TimeSpan? Time { get; set; }
        }
    
        public static void Go()
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(File.ReadAllText(htmlFilePath));
    
            Regex timeRegex = new Regex(@"\b(\d+)h\b\s*\b(\d+)m\b", RegexOptions.Compiled);
            Regex priceRegex = new Regex(@"\b\$?(\d+\.\d\d)\b", RegexOptions.Compiled);
    
            List<Game> gamesList =
                doc.DocumentNode.SelectNodes("//table[@class='table game-table']/tbody/tr")
                    .Select(tr => tr.SelectNodes("td")?.ToList())
                    .Where(tds => tds != null && tds.Any())
                    .Select(
                        (tds) =>
                        {
                            return
                                new
                                {
                                    Img = tds[0].SelectSingleNode("img").Attributes["src"].Value,
                                    Name = tds[1].InnerText,
                                    PriceString = tds[2].InnerText,
                                    PriceMatch = priceRegex.Match(tds[2].InnerText),
                                    TimeString = tds[3].InnerText,
                                    TimeMatch = timeRegex.Match(tds[3].InnerText),
                                };
                        })
                    .Select(
                        (v) =>
                        {
                            Game game =
                                new Game
                                {
                                    Img = v.Img,
                                    Name = v.Name,
                                };
    
                            //Set Price
                            if (v.PriceMatch.Success)
                            {
                                game.Price = double.Parse(v.PriceMatch.Groups[1].Value);
                                game.PriceStatus = "In Store";
                            }
                            else
                            {
                                game.PriceStatus = v.PriceString;
                            }
    
                            //Set Time
                            if (v.TimeMatch.Success)
                            {
                                int hours = int.Parse(v.TimeMatch.Groups[1].Value);
                                int minutes = int.Parse(v.TimeMatch.Groups[2].Value);
    
                                game.Time = new TimeSpan(hours, minutes, 0);
                            }
    
                            return game;
                        })
                    .ToList();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 2021-07-01
      • 2019-01-16
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 2017-11-26
      相关资源
      最近更新 更多