I am using XPath to scraping any web page which I want. For my experiment, I am taking a google web page, you can take any other.
For scraping, I did use the library Fuzi recommended by Eric Aya
For installation, I have used Swift Package Manager because with Cocoa Pods and Carthage I can't install this library, I don't know why)
For testing I am using this link:
https://www.google.com/search?q=what+is+swift+programming+language&oq=what+is+swift+programming+lan&aqs=chrome.1.69i57j0l3j0i22i30l5j0i390.9399j0j7&sourceid=chrome&ie=UTF-8
From the webpage above I have scraped, titles, urs, and, description.
My code:
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
parseHTML()
}
func parseHTML() {
browser.evaluateJavaScript("document.documentElement.outerHTML") { (result, error) in
guard let html = result as? String, error == nil else {
print("Failed to get html string")
return
}
do {
// if encoding is omitted, it defaults to NSUTF8StringEncoding
let doc = try HTMLDocument(string: html, encoding: String.Encoding.utf8)
let classNameTitle = "LC20lb DKV0Md"
let classNameDescription = "aCOpRe"
let classNameURL = "yuRUbf"
// XPath queries
print("\n TITLE \n")
for script in doc.xpath("//h3[@class='\(classNameTitle)']") {
print(script.stringValue)
}
print("\n Description \n")
for script in doc.xpath("//span[@class='\(classNameDescription)']") {
print(script.stringValue)
}
print("\n URL \n")
for script in doc.xpath("//div[@class='\(classNameURL)']") {
let a = script.firstChild(xpath: "a[contains(@href, 'http')]")
print(String((a?.attr("href") ?? "Something goes wrong, oops, sorry :(")))
}
} catch let error {
print(error)
}
}
}
}
Also for the WKWebView, I am installed a desktop user-agent:
lazy var browser: WKWebView = {
let view = WKWebView()
view.navigationDelegate = self
view.customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36"
return view
}()
I don't describe all syntax of XPath, because I use XPath for the first time :) Link to the syntax of XPath, I recommend reading the full XPath tutorial.
My output of the webpage:
TITLE
Swift - Apple Developer
Pros and Cons of Swift Programming Language | AltexSoft
Swift (мова програмування) — Вікіпедія
Swift (programming language) - Wikipedia
About Swift — The Swift Programming Language (Swift 5.4)
About Swift - Swift.org
What Is Swift Programming Language and Why Should You ...
Introduction and history of Swift up to 2020 - Exyte
What is the Swift programming language, and why should I ...
What is the Swift programming language? | by Lena Charles ...
Description
Great First Language — Swift is a powerful and intuitive programming language for macOS, iOS, watchOS, tvOS, and beyond. Writing Swift code is interactive and fun, the syntax is concise yet expressive, and Swift includes modern features developers love. Swift code is safe by design, yet also produces software that runs lightning-fast.
29 серп. 2019 р. — Swift is a compiled programming language for iOS, macOS, watchOS, tvOS, and Linux applications. Here's what you need to know about Swift.
↑ Apple announces Swift, a new programming language for iOS. ↑ The Swift Programming Language; ↑ Lattner, Chris (June 3, 2014) ...
Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. and the open-source community, first released in 2014.
It's a safe, fast, and interactive programming language that combines the best in modern language thinking with wisdom from the wider Apple engineering ...
About Swift. Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns. The goal of ...
29 вер. 2020 р. — Swift was created by Apple in 2014 for iOS, watchOS, tvOS, macOS app development as a logical substitute for Objective-C, which had flaws ...
20 лист. 2020 р. — What is Swift? First released in 2014, Swift is a powerful general-purpose programming language for the Apple ecosystem. Whether you need to ...
Swift, often referred to as “Objective-C, without the C", is an open source programming language developed and maintained by Apple, and it's what the company ...
Swift is the modern programming language that was designed to overcome the several challenges of the versatile Apple programming language Objective C. It ...
URL
https://developer.apple.com/swift/
https://www.altexsoft.com/blog/engineering/the-good-and-the-bad-of-swift-programming-language/
https://uk.wikipedia.org/wiki/Swift_(%D0%BC%D0%BE%D0%B2%D0%B0_%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D1%83%D0%B2%D0%B0%D0%BD%D0%BD%D1%8F)
https://en.wikipedia.org/wiki/Swift_(programming_language)
https://docs.swift.org/swift-book/
https://swift.org/about/
https://blackthorn-vision.com/blog/what-is-swift-programming-language-and-why-should-you-use-it
https://exyte.com/blog/introduciton-to-swift
https://www.itpro.co.uk/development/34417/what-is-the-swift-programming-language-and-why-should-i-learn-it
https://lenac1884.medium.com/what-is-the-swift-programming-language-b45e271175e2
If someone can give me advice above my code and etc. I will be appreciated :)