重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create','UA-11940834-2','v2ex.com');
ga('send','pageview');
显示的web图片在上传文件中。
解析的过程如下:
/**
获取我的收藏帖子列表
*/
class func getFavoriteList(_ page:Int = 1, completionHandler: @escaping (V2ValueResponse<([TopicListModel],Int)>) -> Void){
//先获取请求地址,如: https://www.v2ex.com/my/topics?p=1
Alamofire.request(V2EXURL+"my/topics?p=\(page)", headers: MOBILE_CLIENT_HEADERS).responseJiHtml { (response) -> Void in
var resultArray:[TopicListModel] = []
var maxPage = 1
if let jiHtml = response.result.value {
// 先找到 class="cell item" 的这个节点
if let aRootNode = jiHtml.xPath("//*[@class='cell item']"){
for aNode in aRootNode {
let topic = TopicListModel(favoritesRootNode:aNode)
resultArray.append(topic);
}
}
//更新通知数量
V2User.sharedInstance.getNotificationsCount(jiHtml.rootNode!)
//获取最大页码 只有第一页需要获取maxPage
if page <= 1
,let aRootNode = jiHtml.xPath("//*[@class='page_normal']")?.last
, let page = aRootNode.content
, let pageInt = Int(page)
{
maxPage = pageInt
}
}
let t = V2ValueResponse<([TopicListModel],Int)>(value:(resultArray,maxPage), success: response.result.isSuccess)
completionHandler(t);
}
}
//////////////////////////////
if let aRootNode = jiHtml.xPath("//*[@class='cell item']"){
for aNode in aRootNode {
let topic = TopicListModel(favoritesRootNode:aNode)
resultArray.append(topic);
}
}
这部分是主要的,找到节点。
init(favoritesRootNode:JiNode) {
super.init()
self.avata = favoritesRootNode.xPath("./table/tr/td[1]/a[1]/img[@class='avatar']").first?["src"]
self.nodeName = favoritesRootNode.xPath("./table/tr/td[3]/span[2]/a[1]").first?.content
self.userName = favoritesRootNode.xPath("./table/tr/td[3]/span[2]/strong[1]/a").first?.content
let node = favoritesRootNode.xPath("./table/tr/td[3]/span/a[1]").first
self.topicTitle = node?.content
self.setupTitleLayout()
var topicIdUrl = node?["href"];
if var id = topicIdUrl {
if let range = id.range(of: "/t/") {
id.replaceSubrange(range, with: "");
}
if let range = id.range(of: "#") {
id = id.substring(to: range.lowerBound)
topicIdUrl = id
}
}
self.topicId = topicIdUrl
let date = favoritesRootNode.xPath("./table/tr/td[3]/span[2]").first?.content
if let date = date {
let array = date.components(separatedBy: "")
if array.count == 4 {
self.date = array[3].trimmingCharacters(in: NSCharacterSet.whitespaces)
}
}
self.lastReplyUserName = favoritesRootNode.xPath("./table/tr/td[3]/span[2]/strong[2]/a[1]").first?.content
self.replies = favoritesRootNode.xPath("./table/tr/td[4]/a[1]").first?.content
}
这个方法里面代码的作用是解析得到cell中模型的数据。比如cell中的一个模型数据为userName,有一个值为lishunli,根据favoritesRootNode.xPath("./table/tr/td[3]/span[2]/strong[1]/a").first?.content后进行赋值。
解析后赋值给模型的图片在上传文件中。