如何从新闻网站获取标题名称?
How to get titles name from a news website?
我想从没有 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()
}
问题是,我无法获得标题值-
您得到 url 的响应是 String
然后只需将字符串解析为子字符串。
例如标题是var title =
和var brief =
之间的子字符串。
split
或components(separatedBy:_)
等String方法都可以做到
如果您熟悉 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([=12=].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\""
]
我想从没有 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()
}
问题是,我无法获得标题值-
您得到 url 的响应是 String
然后只需将字符串解析为子字符串。
例如标题是var title =
和var brief =
之间的子字符串。
split
或components(separatedBy:_)
等String方法都可以做到
如果您熟悉 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([=12=].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\""
]