简单介绍
<h2>新手标 TTB1106</h2><h2> TTB1107 </h2><h2> TTB1108</h2>
假如我要获取html中h2的字符串
//
var stringNeedToCatch="<h2>新手标 TTB1106</h2><h2>新手标 TTB1107 </h2><h2>新手标 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(" ")?.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