如何从新闻网站获取标题名称?

时间:2017-11-20 07:49:01

标签: swift nsurlsession nsurl

我想从没有RSS Feed的新闻网站获取数据,我只想获得标题名称,而我正在使用此代码 -

var url = NSURL(string: "http://www.gulf-times.com/stories/c/192/0/Sport")

        if url != nil {
            let task = URLSession.shared.dataTask(with: url! as URL, completionHandler: { (data, response, error) -> Void in
                print(data)

                if error == nil {

                    var urlContent = NSString(data: data!, encoding: String.Encoding.ascii.rawValue) as NSString!

                    print(urlContent)
                }
            })
            task.resume()
        }

问题是,我无法获得标题值

enter image description here

2 个答案:

答案 0 :(得分:1)

如果您对RegEx感到满意,请使用以下模式

/title = "(.*)"/g

这将为您提供所有标题。

<强>修改:

请使用如下

let matched = matches(for: "title = \"(.*)\"", in: contentOfPage)

匹配:功能

func matches(for regex: String, in text: String) -> [String] {

    do {
        let regex = try NSRegularExpression(pattern: regex)
        let results = regex.matches(in: text,
                                    range: NSRange(text.startIndex..., in: text))
        return results.map {
            String(text[Range($0.range, in: text)!])
        }
    } catch let error {
        print("invalid regex: \(error.localizedDescription)")
        return []
    }
}

获得以下结果

[  
   "title = \"Al-Khelaifi voted Asian tennis Chairman\"",
   "title = \"Dimitrov downs Goffin for ATP Tour Finals crown\"",
   "title = \"Coach Lehmann calls for Australia to get behind Ashes selection\"",
   "title = \"Federer expects great things from returning trio\"",
   "title = \"Whateley wins Air Maroc League second stage\"",
   "title = \"Qatar-based Frijns finishes strong as Oliphant wins\"",
   "title = \"Sutton faces tough road ahead to get Chinese on track\"",
   "title = \"Qatar, Japan sign deal to import, export race horses\"",
   "title = \"Islanders deny Lightning comeback for third straight win\"",
   "title = \"Curry leads Golden Warriors fightback after Sixers blitz\"",
   "title = \"Fleetwood claims European Order of Merit as Rose falters\"",
   "title = \"Challengers win thriller against City Exchange\""
]

答案 1 :(得分:0)

你得到url的响应是String然后只是将字符串解析为子字符串。

例如,标题是var title =var brief =之间的子字符串。

splitcomponents(separatedBy:_)等字符串方法可以实现。