Results 1 to 6 of 6

Thread: Filtering html to get results

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    Filtering html to get results

    I'm working on .net6+ winForms.
    I'm trying to filter results by some criteria having a big html. I don't know if what I am trying to ask is achievable, but it costs nothing to ask. I'm playing a browser game which as a big map 100x100 and for each pixel it has an island. Each island has a different cave ( of different materials, but I am only interested on a determined material ) and a carpentry and each island has 16 free slots. I would like to find an island with no more than 7 occupied slots and of these, which has the highest level of carpentry and mine. Each island html is structured like this :
    Code:
    <div id="tile_12_10" class="islandTile island2" style="left: 240px; top: 1320px; display: block;" title="Nyeos [71:92]">
       <div id="marking_12_10" class=""></div>
       <div id="wonder_12_10" class="wonder wonder1"><span class="ikaeasy-resource ikaeasy-resource-wonder ikaeasy-t-20 ikaeasy-l--20 ikaeasy-d-n">5</span></div>
       <div id="tradegood_12_10" class="tradegood tradegood2"><span class="ikaeasy-resource ikaeasy-resource-mine ikaeasy-l--20 ikaeasy-d-n">16</span></div>
       <div id="cities_12_10" class="cities">16</div>
       <div id="piracy_12_10" class=""></div>
       <div id="helios_12_10" class=""></div>
       <div id="magnify_12_10"></div>
       <div id="owner_12_10" class="ownerState "></div>
       <a href="javascript:ikariam.getScreen().clickIsland('tile_12_10');" id="linkurl_12_10" class="linkurl"></a>
       <div class="ikaeasy-resource ikaeasy-resource-wood ikaeasy-b-70 ikaeasy-l-55 ikaeasy-d-n">
          <div class="ikaeasy-resource-icon ikaeasy-w-24 ikaeasy-h-20" style="background-image: url(skin/resources/icon_wood.png);"></div>
          <span>21</span>
       </div>
    </div>
    The results I'm interested are: For the cave, as I said, I'm only interested to find the island with a specific material, and the latter is specified in the html in the class tradegood tradegood2. So that "2" is the specific materials the island produces. After that, the level of cave is determine in the class ikaeasy-resource ikaeasy-resource-mine ikaeasy-l--20 ikaeasy-d-n which contains the "16" level. For the wood( carpentry ), the div class ikaeasy-resource ikaeasy-resource-wood ikaeasy-b-70 ikaeasy-l-55 ikaeasy-d-n which contains the "21" level. For the occupied slots, the div cities_12_10 which contains class="cities" which is "16", which means the island has not free slots.

    Would be possible to get all the island that has that specific material, no more than 7 slots occupied, and to sort these for the highest cave and carpentry levels? something like
    Island [71:92] mine : lvl 16 carpentry : lvl 20 free slots: 9
    p.s. The class class="oceanTile " is pure ocean so no island in there.
    p.s.2 since <div id="tile_12_10" changes for every island, i'm adding and starts-with(@id, 'tile_')]") at the first for each. I'm using html agility pack library and this is what i've been able to write so far
    I'm using html agility pack library and this is what i've been able to write so far

    Code:
    Imports System.IO
    Imports HtmlAgilityPack
    
    Public Class Form1
        Public Class Island
            Public Property Coord As String
            Public Property MineLevel As Integer
            Public Property CarpentryLevel As Integer
            Public Property FreeSlots As Integer
        End Class
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
              Dim htmlDoc As New HtmlAgilityPack.HtmlDocument()
            htmlDoc.Load("pastebin.html")
    
                ' Get all islandTile elements that have a trade good type of 2 (tradegood2)
    
            Dim islands As New List(Of Island)()
            For Each tile As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//div[contains(@class, 'islandTile') and not(contains(@class, 'oceanTile')) and starts-with(@id, 'tile_')]")
    
                Dim tradeGoodTypeNode As HtmlNode = tile.SelectSingleNode(".//div[contains(@class, 'tradegood')]/@class")
                Dim tradeGoodType As String
                If tradeGoodTypeNode IsNot Nothing Then
                    tradeGoodTypeNode.GetAttributeValue("class", "")
                  
                     ' Get the mine and carpentry levels
                    Dim mineLevel As Integer
                    Integer.TryParse(tile.SelectSingleNode(".//div[contains(@class, 'tradegood2')]/span")?.InnerText, mineLevel)
                    Dim carpentryLevel As Integer
                    Integer.TryParse(tile.SelectSingleNode(".//div[contains(@class, 'ikaeasy-resource-wood')]/span")?.InnerText, carpentryLevel) ' Get the number of occupied slots
                    Dim occupiedSlots As Integer
                    Integer.TryParse(tile.SelectSingleNode(".//div[contains(@class, 'cities')]/text()")?.InnerText, occupiedSlots)  ' If the island has less than 7 occupied slots, add it to the list
                    If occupiedSlots <= 7 Then
                        islands.Add(New Island() With {
                            .Coord = tile.GetAttributeValue("id", ""),
                            .MineLevel = mineLevel,
                            .CarpentryLevel = carpentryLevel,
                            .FreeSlots = 16 - occupiedSlots
                        })
                    End If
                End If
    
            Next
    
               ' Sort islands by mine and carpentry level
            islands = islands.OrderByDescending(Function(x) x.MineLevel).ThenByDescending(Function(x) x.CarpentryLevel).ToList()
    
                ' Print island information
    
            For Each island As Island In islands
                Console.WriteLine($"Island {island.Coord} mine : lvl {island.MineLevel} carpentry : lvl {island.CarpentryLevel} free slots: {island.FreeSlots}")
            Next
        End Sub
    End Class
    but nothing happens when i press the button...

    I pasted the entire html in a pastebin file if it could give any help. Much appreciated
    Last edited by matty95srk; Feb 27th, 2023 at 08:27 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width