如何在iOS Swift 3中从url解析HTML后获取表数据
我正在开发一个iOS应用程序,其中我需要从带有KannaAPI的链接解析html。我已经做到了。但是它显示了所有的html文件。我只需要显示一部分这样的数据如何在iOS Swift 3中从url解析HTML后获取表数据,html,ios,swift,Html,Ios,Swift,我正在开发一个iOS应用程序,其中我需要从带有KannaAPI的链接解析html。我已经做到了。但是它显示了所有的html文件。我只需要显示一部分这样的数据 <table width="880" border="1" cellspacing="0" cellpadding="0"> <tr> <td width="81"><strong>Trip Name </strong></td> <t
<table width="880" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="81"><strong>Trip Name </strong></td>
<td width="159"><div align="center"><strong>Starting Time from Campus </strong></div></td>
<td width="186"><div align="center"><strong>Starting Spot & Time </strong></div></td>
<td width="444"><strong>Remarks</strong></td>
</tr>
<tr>
<td><div align="center">Normal-1</div></td>
<td><div align="center">6:30 AM </div></td>
<td>Rupsha, 7:20 AM </td>
<td>Will back via Royalmore & Ferighat </td>
</tr>
<tr>
<td><div align="center">Normal-1</div></td>
<td><div align="center">6:45 AM </div></td>
<td>Moylapota, 7:25 AM </td>
<td>Will back via Shibbari - Sonadangha </td>
</tr>
</table>
行程名称
从校园开始的时间
起点&;时间
备注
正常-1
上午6:30
鲁普沙,上午7:20
将通过Royalmore&;费里格特
正常-1
上午6:45
莫伊拉波塔,上午7:25
将通过Shibbari-Sonadangha返回
我在swift 3中使用了这部分代码来显示所有html数据
let html = "<html>...</html>"
if let doc = HTML(html: html, encoding: .utf8) {
// Search for nodes by XPath
for link in doc.xpath("//a | //link") {
print(link["href"])
}
}
let html=“…”
如果let doc=HTML(HTML:HTML,编码:.utf8){
//通过XPath搜索节点
对于doc.xpath中的链接(“//a |//link”){
打印(链接[“href”])
}
}
现在我只想获取html数据的表部分。我该怎么做呢?这就可以了:
//I replaced " with ' to make it easier. But it shouldn't be a problem in your case.
let html = "<table width='880' border='1' cellspacing='0' cellpadding='0'>"
+ "<tr>"
+ "<td width='81'><strong>Trip Name </strong></td>"
+ "<td width='159'><div align='center'><strong>Starting Time from Campus </strong></div></td>"
+ "<td width='186'><div align='center'><strong>Starting Spot & Time </strong></div></td>"
+ "<td width='444'><strong>Remarks</strong></td>"
+ "</tr>"
+ "<tr>"
+ "<td><div align='center'>Normal-1</div></td>"
+ "<td><div align='center'>6:30 AM </div></td>"
+ "<td>Rupsha, 7:20 AM </td>"
+ "<td>Will back via Royalmore & Ferighat </td>"
+ "</tr>"
+ "<tr>"
+ "<td><div align='center'>Normal-1</div></td>"
+ "<td><div align='center'>6:45 AM </div></td>"
+ "<td>Moylapota, 7:25 AM </td>"
+ "<td>Will back via Shibbari - Sonadangha </td>"
+ "</tr>"
+ "</table>"
var infoFromHTML : [[String]] = []
if let doc = try? HTML(html: html, encoding: .utf8) {
var row : [String] = []
//look for td tags, not a or link tags
for (index, td) in doc.css("td").enumerated(){
row.append(td.text as! String)
if index%4 == 3{
//if all row text are added to the row array, append it to info, and reset
infoFromHTML.append(row)
row = []
}
}
}
print(infoFromHTML)
若不想使用表的标题,只需假装数组的第一个元素不是0,而是1
如果我们谈论为什么你不能让它工作;您正在这里查找a
和link
标记doc.xpath(“//a |//link”)
。但是你有td
标签。所以您应该这样做:doc.css(“td”)
。我使用了css函数,但也可以使用XPath
祝你好运
编辑:您仍然应该请求API。最好是RESTful JSON API
编辑2:我使用了Swift 4版本的卡纳。您可能需要做一些更改才能将其用于Swift 3。我想您获得的HTML网站不提供API。?如果网站是你的;你的路不好走,伙计。您必须开发一个API并使用它来获取数据。你不应该获取HTML并解析它。HTML不提供JSON文件。我想解析我的应用程序中的一些数据,然后你应该向网站所有者索取API。这是他们应该(必须)提供的。但是如果你不能要求它,让我知道,然后我会创建一个项目,并试图解决你的问题。我已经有4年没有解析HTML了。你肯定应该找一个API。几乎所有的现代网站都有,或者如果没有,要么是因为它们太小,要么是因为它们不希望人们窃取它们的内容。如果你必须刮网站,至少使用类似的东西。网站所有者没有给我api。所以我必须通过网络营销来做到这一点。如果答案解决了你的问题,请检查它是否被接受。非常感谢:)请修正我的问题
[
["Trip Name ", "Starting Time from Campus ", "Starting Spot & Time ", "Remarks"],
["Normal-1", "6:30 AM ", "Rupsha, 7:20 AM ", "Will back via Royalmore & Ferighat "],
["Normal-1", "6:45 AM ", "Moylapota, 7:25 AM ", "Will back via Shibbari - Sonadangha "]
]