Created
November 24, 2015 10:02
-
-
Save yuichirokato/fa15a450bc2cc3672b62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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