gpt4 book ai didi

ios - 快速HTML解析

转载 作者:搜寻专家 更新时间:2023-11-01 05:42:24 25 4
gpt4 key购买 nike

我正在将html内容从 objective-c 解析为swift作为的引用http://www.raywenderlich.com/14172/how-to-parse-html-on-ios

    var tutorialsURL = NSURL(string: "http://www.raywenderlich.com/tutorials")
var tutorialsHTMLData: NSData = NSData(contentsOfURL: tutorialsURL)
var tutorialsParser = TFHpple(HTMLData: tutorialsHTMLData)
var tutorialsXPathString = "//div[@class='content-wrapper']/ul/li/a"
var tutorialNodes = tutorialsParser.searchWithXPathQuery(tutorialsXPathString)
var newTutorials: NSMutableArray = [0]

for element in tutorialNodes
{
var elementTwo: TFHppleElement = element as TFHppleElement
var tutorial = Tutorial()

println("my data in element2 \(elementTwo)")
println("\(tutorial)")
newTutorials.addObject(tutorial)

tutorial.title = elementTwo.firstChild.content as String!

tutorial.url = elementTwo.objectForKey("href")


}
objects = newTutorials
tableView.reloadData()

}

但是当我运行此命令时,我遇到了类似于 的致命错误:意外地发现nil展开了一个可选值

有人帮我

最佳答案

经过一番艰苦的努力,我找到了问题的解决方案,节点问题** var tutorialsXPathString =“// div [@ class ='content-wrapper'] / ul / li / a” **没有正确给出,
我尝试了另一个网址,并正确指定了searchpathstring,问题已解决,

   func loadTutorial()
{

var tutorialsURL = NSURL(string: "http://www.worldgeneralknowledge.com/important_books_and_aurther.html")
var htmlData: NSData = NSData(contentsOfURL: tutorialsURL)
var tutorialsParser = TFHpple(HTMLData: htmlData)
var tutorialsXPathString = "//table/tr/td[@class=\"tr_dark\"]"
var tutorialNodes = tutorialsParser.searchWithXPathQuery(tutorialsXPathString) as NSArray
if(tutorialNodes.count == 0)
{
println("empty here")
}
else
{
for element in tutorialNodes
{
var elementTwo: TFHppleElement = element as TFHppleElement
var tutorial = Tutorial()
println("\(elementTwo.firstChild.content)")
tutorial.title = elementTwo.firstChild.content as String!
objects.addObject(tutorial.title)
}
tableView.reloadData()

}
}

如果需要,还可以通过将其作为搜索路径字符串来搜索更多节点,并且可以将它们添加到单个数组中,然后可以打印内容。

谢谢。

关于ios - 快速HTML解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26798005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com