Trying to retrieve: "17,02" from the HTML below:
<div class="overflow-auto">
<table class="w-100 tl mb4 mt3 f6" cellspacing="0">
<thead>
<tr>
<th class="fw6 bb b--black-20 tl pb3 pr3 bg-white tl">Kvalitet</th>
<th class="fw6 bb b--black-20 tl pb3 pr3 bg-white tl">Pris inkl. mva.</th>
<th class="fw6 bb b--black-20 tl pb3 pr3 bg-white tl">Endring</th>
<th class="fw6 bb b--black-20 tl pb3 pr3 bg-white tl">Gjeldene fra</th>
</tr>
</thead>
<tbody class="lh-copy">
<tr>
<td class="pv3 pr3 bb b--black-20"><img src="./assets/95 Miles.png" alt="95 Miles"></td>
<td class="pv3 pr3 bb b--black-20">Kr 17,02</td>
<td class="pv3 pr3 bb b--black-20">5 øre</td>
<td class="pv3 pr3 bb b--black-20">24.08.2018</td>
</tr>
</tbody>
<tbody class="lh-copy">
<tr>
<td class="pv3 pr3 bb b--black-20"><img src="./assets/D Miles.png" alt="D Miles"></td>
<td class="pv3 pr3 bb b--black-20">Kr 15,80</td>
<td class="pv3 pr3 bb b--black-20">5 øre</td>
<td class="pv3 pr3 bb b--black-20">24.08.2018</td>
</tr>
</tbody>
<tbody class="lh-copy">
<tr>
<td class="pv3 pr3 bb b--black-20"><img src="./assets/95 Miles Plus.png" alt="95 Miles"></td>
<td class="pv3 pr3 bb b--black-20">Kr 18,01</td>
<td class="pv3 pr3 bb b--black-20">5 øre</td>
<td class="pv3 pr3 bb b--black-20">24.08.2018</td>
</tr>
</tbody>
<tbody class="lh-copy">
<tr>
<td class="pv3 pr3 bb b--black-20"><img src="./assets/D Miles Plus.png" alt="D Miles"></td>
<td class="pv3 pr3 bb b--black-20">Kr 16,79</td>
<td class="pv3 pr3 bb b--black-20">5 øre</td>
<td class="pv3 pr3 bb b--black-20">24.08.2018</td>
</tr>
</tbody>
</table>
</div>
I've tried using this code in swift:
let titles = try doc.getElementsByClass("pv3 pr3 bb b--black-20").array()
But when I try to print it out I get nil back. Does anyone have any solutions or ideas?
To select an element that belongs to at least one of many classes, separate these classes with a comma:
Use this to select the second
td:The selector "tbody tr td" looks for all
tds inside atrinside of atbody. And then we know that the secondtdis the one we want. So, we convert the result to an array, and then the we select the second element in that array by using the subscript[1].If you're sure you want just the second td in your html document, then the selector could be shortened :
If you want to get all the second
tds in your table which text starts with "Kr " :If you want the text of these
tds but without "Kr " :Here is the final code:
And it prints
["17,02", "15,80", "18,01", "16,79"].For more documentation on how to use SwiftSoup, have a look here.