Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env xcrun swift -F Carthage/Build/Mac/
import Foundation
import Alamofire
import SwiftScriptRunner
import Kanna
import Bond
let scrapeUrl = "http://gamefavo.com/db/splatoon/weapon-list/"
let html = Observable(HTML(html: NSData(), encoding: NSUTF8StringEncoding))
func getHtml() {
Alamofire.request(.GET, scrapeUrl).responseData { response in
if let data = response.data {
html.next(HTML(html: data, encoding: NSUTF8StringEncoding))
}
}
}
let runner = SwiftScriptRunner()
runner.lock()
getHtml()
html.observe { html in
let tableCSS = "table"
let css = "h2.m30"
let jenres = html?.css(css).map { $0.text ?? "" }.filter { $0 != "" } ?? []
let bukiList = html?.css(tableCSS).map { node -> [String] in
node.css("tr").map { $0.css("td").at(2)?.text ?? "" }.filter { $0 != "" }
}.filter { $0.count > 0 } ?? [[]]
let result = zip(jenres, bukiList).map { (jenre, buki) in
return [jenre: buki]
}
print("\(jenres)")
print("\(result)")
if html != nil {
runner.unlock()
}
}
runner.wait()
github "Alamofire/Alamofire" ~> 3.0
github "mgrebenets/SwiftScriptRunner"
github "SwiftBond/Bond"
github "tid-kijyun/Kanna" >= 0.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment