NSLog("开始获取url")
var URLARR = [String]()
var count = 200
for i in 1...count/20{
println("\(i)page")
let urlString = "http://b2b.huangye88.com/fuzhou/tuliao/pn\(i)"
let request = NSURL(string: urlString)
let data = NSData(contentsOfURL: request!)
let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
let bigScanner = NSScanner(string: dataString as String)
var bigString:NSString?
while !bigScanner.atEnd{
bigScanner.scanUpToString("<dt><h4><a href=\"", intoString: nil)
bigScanner.scanUpToString("\" ", intoString: &bigString)
let string = bigString!.stringByReplacingOccurrencesOfString("<dt><h4><a href=\"", withString: "")
URLARR.append(string)
}
}
NSLog("获取url结束")
var INFOS = ""
NSLog("开始获取信息")
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
let group = dispatch_group_create()
if URLARR.count >= count{
for num in 0..<20{
dispatch_group_async(group, queue, { () -> Void in
for i in num*(count/20)..<(num+1)*(count/20){
let request = NSURL(string: URLARR[i])
let data = NSData(contentsOfURL: request!)
if data != nil{
let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
//先截取信息体
let infoScanner = NSScanner(string: dataString as String)
var infoStr:NSString?
infoScanner.scanUpToString("<div class=\"Contact\">", intoString: nil)
infoScanner.scanUpToString("</div><!--Contact-->", intoString: &infoStr)
var tempInfo = ""
if infoStr != nil{
//公司名
var componyScanner = NSScanner(string: infoStr! as String)
var componyStr:NSString?
componyScanner.scanUpToString("公司名称:", intoString: nil)
componyScanner.scanUpToString("</a>", intoString: &componyStr)
var componyScannerA = NSScanner(string:componyStr! as String)
var componyScannerAStr:NSString?
componyScannerA.scanUpToString("title=\"", intoString: nil)
componyScannerA.scanUpToString("\">", intoString: &componyScannerAStr)
let COMPONY = componyScannerAStr!.stringByReplacingOccurrencesOfString("title=\"", withString: "")
tempInfo += "公司名称:" + COMPONY + "\n"
//联系电话
var phoneScanner = NSScanner(string: infoStr! as String)
var phoneStr:NSString?
phoneScanner.scanUpToString("联系人手机:", intoString: nil)
phoneScanner.scanUpToString("/span>", intoString: &phoneStr)
if phoneStr == nil{
var phoneScannerB = NSScanner(string: infoStr! as String)
var phoneStrB:NSString?
phoneScannerB.scanUpToString("公司电话:", intoString: nil)
phoneScannerB.scanUpToString("td>", intoString: &phoneStrB)
if phoneStrB == nil {
tempInfo += "公司电话:" + "无" + "\n"
}else{
var phoneScannerA = NSScanner(string: phoneStrB! as String)
var phoneStrA:NSString?
phoneScannerA.scanUpToString("boldred\">", intoString: nil)
phoneScannerA.scanUpToString("<", intoString: &phoneStrA)
let PHONE = phoneStrA!.stringByReplacingOccurrencesOfString("boldred\">", withString: "")
tempInfo += "公司电话:" + PHONE + "\n"
}
}else{
var phoneScannerA = NSScanner(string: phoneStr! as String)
var phoneStrA:NSString?
phoneScannerA.scanUpToString("boldred\">", intoString: nil)
phoneScannerA.scanUpToString("<", intoString: &phoneStrA)
let PHONE = phoneStrA!.stringByReplacingOccurrencesOfString("boldred\">", withString: "")
tempInfo += "联系人手机:" + PHONE + "\n"
}
//联系人名字
var nameScanner = NSScanner(string: infoStr! as String)
var nameStr:NSString?
nameScanner.scanUpToString("联系人:", intoString: nil)
nameScanner.scanUpToString("/span>", intoString: &nameStr)
if nameStr == nil {
tempInfo += "联系人:" + "无" + "\n\n"
}else{
var nameScannerA = NSScanner(string: nameStr! as String)
var nameStrA:NSString?
nameScannerA.scanUpToString("span>", intoString: nil)
nameScannerA.scanUpToString("<", intoString: &nameStrA)
let NAME = nameStrA!.stringByReplacingOccurrencesOfString("span>", withString: "")
tempInfo += "联系人:" + NAME + "\n\n"
}
INFOS += tempInfo
println(tempInfo)
}
}
}
})
}
dispatch_group_notify(group, queue) { () -> Void in
NSLog("获取信息结束")
NSLog("开始写入文件")
//文件写入jinrong
let file = "Infos.txt"
if let dirs : [String] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] {
let dir = dirs[0]
let path = dir.stringByAppendingPathComponent(file);
println(path)
INFOS.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding, error: nil);
}
NSLog("写入文件结束")
}
}else{
println("URLARR.count->\(URLARR.count)")
}
网友评论