在swift中解析html的最佳做法是什么?

前端之家收集整理的这篇文章主要介绍了在swift中解析html的最佳做法是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是一个Swift新手我需要像Swift iOS项目中的Python的BeautifulSoup这样的东西。确切地说,我需要得到< a>以“.txt”结尾。我应该采取什么步骤?
有几个很好的HTML解析库,使用Swift和Objective-C,如下所示:

> hpple
> NDHpple
> Kanna( old Swift-HTML-Parser)
> Fuzi

看看下面的例子,在上面发布的四个图书馆里,主要使用XPath 2.0进行解析:

hpple:

let data = NSData(contentsOfFile: path)
let doc = TFHpple(HTMLData: data)

if let elements = doc.searchWithXPathQuery("//a/@href[ends-with(.,'.txt')]") as? [TFHppleElement] {
   for element in elements {
       println(element.content)
   }
}

NDHpple:

let data = NSData(contentsOfFile: path)!
let html = NSString(data: data,encoding: NSUTF8StringEncoding)!
let doc = NDHpple(HTMLData: html)
if let elements = doc.searchWithXPathQuery("//a/@href[ends-with(.,'.txt')]") {
   for element in elements {
     println(element.children?.first?.content)
   }
}

Kanna (Xpath and CSS Selectors):

let html = "<html><head></head><body><ul><li><input type='image' name='input1' value='string1value' class='abc' /></li><li><input type='image' name='input2' value='string2value' class='def' /></li></ul><span class='spantext'><b>Hello World 1</b></span><span class='spantext'><b>Hello World 2</b></span><a href='example.com'>example(English)</a><a href='example.co.jp'>example(JP)</a></body>"

if let doc = Kanna.HTML(html: html,encoding: NSUTF8StringEncoding) {
   var bodyNode   = doc.body

   if let inputNodes = bodyNode?.xpath("//a/@href[ends-with(.,'.txt')]") {
      for node in inputNodes {
         println(node.contents)
      }
   }
}

Fuzi (Xpath and CSS Selectors):

let html = "<html><head></head><body><ul><li><input type='image' name='input1' value='string1value' class='abc' /></li><li><input type='image' name='input2' value='string2value' class='def' /></li></ul><span class='spantext'><b>Hello World 1</b></span><span class='spantext'><b>Hello World 2</b></span><a href='example.com'>example(English)</a><a href='example.co.jp'>example(JP)</a></body>"

do {
  // if encoding is omitted,it defaults to NSUTF8StringEncoding
  let doc = try HTMLDocument(string: html,encoding: NSUTF8StringEncoding)

  // XPath queries
  for anchor in doc.xpath("//a/@href[ends-with(.,'.txt')]") {
    print(anchor.stringValue)
  }

} catch let error {
    print(error)
}

结束功能Xpath 2.0的一部分。

我希望这能帮助你。

原文链接:https://www.f2er.com/swift/320462.html

猜你在找的Swift相关文章