我的简单swift爬虫类

前端之家收集整理的这篇文章主要介绍了我的简单swift爬虫类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


简单介绍

<h2>新手标&nbsp;&nbsp;TTB1106</h2><h2>&nbsp;&nbsp;TTB1107 </h2><h2>&nbsp;&nbsp;TTB1108</h2>

假如我要获取html中h2的字符串

//

var stringNeedToCatch="<h2>新手标&nbsp;&nbsp;TTB1106</h2><h2>新手标&nbsp;&nbsp;TTB1107 </h2><h2>新手标&nbsp;&nbsp;TTB1108</h2>"

var headtext="<h2>"

var endText="</h2>"

//



//我的爬虫类


import Foundation




class LANcrawlerCatchHeadTool {


func LANcrawlerCatchHead( LANstringNeedToCatch:String,headtext:String,endText:String)->NSMutableArray{

let dataList:NSMutableArray=[]

var stringNeedToCatch=LANstringNeedToCatch

let temp="s"

let rang=temp.rangeOfString("s")

repeat{

//获取头字符串区间

let headText=stringNeedToCatch.rangeOfString(headtext)

//获取头字符串区间最后一字符串坐标

let headTextLastIndex=headText?.last

//从头字符串区间最后一字符串坐标开始,截取字符串

var substringFromheadTextLastIndex=stringNeedToCatch.substringFromIndex(headTextLastIndex!)

/*

//获取第一个字符串区间

let substringFromheadTextLastIndex0=substringFromheadTextLastIndex.rangeOfString(">")

//获取第一个字符串坐标

let substringFromheadTextLastIndex0Index=substringFromheadTextLastIndex0?.first

//移除第一个字符串

*/

//移除第一个字符串

substringFromheadTextLastIndex.removeRange(rang!)

//获取尾字符串区间

let substringFromheadTextLastIndexLast=substringFromheadTextLastIndex.rangeOfString(endText)

//获取尾字符串区间第一个字符串坐标

let substringFromheadTextLastIndexLastIndex=substringFromheadTextLastIndexLast?.first

// 从尾字符串区间第一个字符串坐标向前截取字符串

var substringFromheadTextLastIndexToEnd=substringFromheadTextLastIndex.substringToIndex(substringFromheadTextLastIndexLastIndex!)

// print(substringFromheadTextLastIndexToEnd)

//=======加一段

substringFromheadTextLastIndexToEnd=substringFromheadTextLastIndexToEnd.substringToIndex((substringFromheadTextLastIndexToEnd.rangeOfString("&nbsp;")?.first)!)

substringFromheadTextLastIndexToEnd=substringFromheadTextLastIndexToEnd.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())//去除空格

stringNeedToCatch.removeRange(stringNeedToCatch.rangeOfString(headtext)!)

stringNeedToCatch.removeRange(stringNeedToCatch.rangeOfString(endText)!)


dataList.addObjectsFromArray([substringFromheadTextLastIndexToEnd])

}

while(stringNeedToCatch.containsString(headtext)&&stringNeedToCatch.containsString(endText))


return dataList

}


}

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

猜你在找的Swift相关文章