1

Right now in my code I have retrieved HTML from a baseball website that displays the stats of a roster of players. There are multiple tables in the HTML and I only want the data from the one posted below. I'm not sure how to parse only that table from the HTML.

/*this code retrieves the HTML and is what I've tried to do */  
if let url = URL(string: "https://www.examplewebsite.com") {
            do {
                let contents = try String(contentsOf: url)
                statsHTML = contents //statsHTML is a string 

                let doc: Document = try SwiftSoup.parse(statsHTML)
                let table : [Element] = try doc.getElementsByTag("div").array() //attempt to parse by div name

                for div in table {
                    let statsTable = try div.getElementById("baseball-hitting-stats-table") //name of table 
                    print(statsTable) //prints all nil values
                }




/*This is a section of the table in HTML code that I want to 
parse*/

<div name="baseball-hitting-stats-table" class="team-stats-table">

<div class="collclubsports-component tab-wrapper"> <h3 
class="stats-title">Hitting Stats</h3><div style="overflow:auto;">   
<table class="collclubsports-component table-reponsive stats- 
 table">

 </thead><tbody class="collclubsports-component active" name="page- 
 1"><tr><td><a href="/league/player/?player=36c85410-e987-4a73- 
 8b36-e8c0d7ff1eed&season=46d3ea9a-a080-4273-befb- 
 58b30c2adb01">John, Doe</a></td><td>13</td><td>2</td><td>7</td> 
 <td>0</td><td>0</td><td>0</td><td>5</td><td>2</td><td>2</td> 
 <td>2</td><td>0</td><td>0.538</td><td>0.600</td><td>0.538</td> 
 <td>1.138</td><td>0</td><td>0</td><td>0</td><td>0</td><td>7</td> 
 <td>0</td><td>15</td></tr> </table>

I retrieved all div elements successfully and used getElementsByID in an attempt to retrieve the table I want but it prints all nil. Thanks for any help.

pigsploof
  • 35
  • 6

1 Answers1

0

You are using name on your div with the name baseball-hitting-stats-table and then trying to use document.getElementById(). name is an older DOM query used with document.all or document.getElementsByName().

I would use document.getElementById() but you need to put an id on the element, not a name.

So just change this line in your markup if you can.
before:
<div name="baseball-hitting-stats-table" class="team-stats-table">
after:
<div id="baseball-hitting-stats-table" class="team-stats-table">

Or if you can't change the markup, you can use
document.getElementsByName('baseball-hitting-stats-table')[0]
or
document.all.baseball-hitting-stats-table.

MrRobboto
  • 722
  • 3
  • 10
  • I am retrieving the HTML from the internet at runtime of the application, therefore I cannot change the markup. In Swift the getElementsByName() function does not seem to exist. Any ideas? – pigsploof Nov 07 '19 at 20:28
  • And I guess document.all won't work because of the hiphens. I'm not familiar enough with swift - I assumed you have the same document API since it looks so close. Can you you use document.all with bracket notation? – MrRobboto Nov 07 '19 at 20:38