如何在iOS上使用XPath进行抓取?

如何在iOS上使用XPath进行抓取?,ios,swift,parsing,web-scraping,xpath,Ios,Swift,Parsing,Web Scraping,Xpath,我试图找到有关如何在iOS平台上使用XPath的信息。在Apple文档中,我找到了有关XPath的信息,该XPath仅在macOS上可用 我的目标是通过XPath来抓取网页,但是我找不到任何关于XPath的信息,而我使用的是HTMLKit 是否有一种方法可以使用XPath脚本在iOS平台上抓取网页?我正在使用XPath抓取我想要的任何网页。在我的实验中,我用的是谷歌网页,你可以用任何其他的 对于刮取,我确实使用了 对于安装,我使用了Swift Package Manager,因为对于Cocoa

我试图找到有关如何在iOS平台上使用XPath的信息。在Apple文档中,我找到了有关XPath的信息,该XPath仅在macOS上可用

我的目标是通过XPath来抓取网页,但是我找不到任何关于XPath的信息,而我使用的是HTMLKit


是否有一种方法可以使用XPath脚本在iOS平台上抓取网页?

我正在使用XPath抓取我想要的任何网页。在我的实验中,我用的是谷歌网页,你可以用任何其他的

对于刮取,我确实使用了 对于安装,我使用了Swift Package Manager,因为对于Cocoa Pods和Carthage,我无法安装此库,我不知道为什么)

对于测试,我使用以下链接:

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
从上面的网页中,我已浏览了、标题、urs和描述

我的代码:

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)
            }
            
        }
    }
}
对于WKWebView,我还安装了桌面用户代理:

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
    }()
我没有描述XPath的所有语法,因为我第一次使用XPath:)链接到的语法,我建议阅读完整的XPath教程

我的网页输出:

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

如果有人能在我的代码等上面给我一些建议,我将不胜感激:)

你可以使用开源库,比如。@Eric Aya,你可以像答案一样发布这个评论吗?你完全解决了我的问题,我想结束这个帖子。我很高兴我帮助了你。我认为一个更好的答案是你发布一个你如何使用Fuzi解决问题的例子。这样的例子对其他人会有帮助,不仅仅是我发布了一个图书馆链接@Eric Aya,好的,我将发布我是如何解决这个问题的)仅供参考,它是刮(刮,刮,刮)不是scrapping@barny,谢谢您的更正:)