0

I have problem that I need to take some information from Website that has not API so i am taking URL and convert it to String , next i use TFHpple to searching with XPath. I am beginner in XPath's so do you how should look XPath to :

<a href=q/?s=wig20>WIG20</a></td><td><span id=aq_wig20_c2>2305.67</span></td><td nowrap><span id=aq_wig20_m1><font id=c1>+0.38%</font></span></td><td></td><td nowrap><span id=aq_wig20_d1>9:23</span></td></tr><tr>
Laxsion12
  • 67
  • 1
  • 8
  • 1
    What you a looking for ? Nice examples are posted by Victor hear http://stackoverflow.com/questions/31080818/what-is-the-best-practice-to-parse-html-in-swift – Oleg Gordiichuk Apr 25 '17 at 07:44
  • I just need to take WIG20 , 2305.67, 0.38%, 9:23 but HTMLString is huge so i want to do it smart – Laxsion12 Apr 25 '17 at 08:04

1 Answers1

0

In my project I am using Kanna it is more easy to use in Swift projects.

Link to GitHub repo.

In Kanna you can fetch info you need in this way:

let html = "<a href=q/?s=wig20>WIG20</a></td><td><span id=aq_wig20_c2>2305.67</span></td><td nowrap><span id=aq_wig20_m1><font id=c1>+0.38%</font></span></td><td></td><td nowrap><span id=aq_wig20_d1>9:23</span></td></tr><tr>"

if let doc = Kanna.HTML(html: html, encoding: String.Encoding.utf8) {
   let bodyNode   = doc.body

   if let inputNodes = bodyNode?.xpath("//a | //span") {
      for node in inputNodes {
          print(node.content)
      }
   }
 }

Response:

Optional("WIG20")
Optional("2305.67")
Optional("+0.38%")
Optional("9:23")

All data is in html elements that are <a> and <span>.

Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100