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.