/// <summary> /// 解析主页的访问用户 /// </summary> /// <returns></returns> public void VisitParser() { this.Login(); Result result = this.Request(this.HomePageUrl); string pageHtml = result.Msg; if (string.IsNullOrWhiteSpace(pageHtml)) { return; } NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(pageHtml); NSoup.Nodes.Element wrapElement = doc.GetElementById("show_style_01"); NSoup.Select.Elements userElementNodes = wrapElement.GetElementsByTag("li"); if (userElementNodes != null && userElementNodes.Count > 0) { //倒序排列,最新的在最后面 IEnumerable <NSoup.Nodes.Element> userElements = userElementNodes.Reverse(); foreach (NSoup.Nodes.Element userElement in userElements) { NSoup.Nodes.Element picElement = GetElementFirst(userElement.GetElementsByClass("pic")); NSoup.Nodes.Element nameElement = GetElementFirst(userElement.GetElementsByClass("user_name")); NSoup.Nodes.Element userInfoElement = GetElementFirst(userElement.GetElementsByClass("user_info")); NSoup.Nodes.Element dateElement = GetElementFirst(userElement.GetElementsByClass("date")); string userName = nameElement == null ? "" : nameElement.Child(0).Text(); string homePage = UriHelper.RemoveParams(nameElement == null ? "" : nameElement.Child(0).Attr("href")); string pic = picElement.Child(0).Child(0).Attr("src"); DateTime date = Convert.ToDateTime(dateElement.Text().Replace("到访:", "")); string[] userInfo = StringHelper.SplitWhiteSpace(userInfoElement.Child(0).Text()); int age = Convert.ToInt32(userInfo[0].Replace("岁", "")); string addr = userInfo.Length > 1 ? userInfo[1] : string.Empty; string userCode = homePage.Substring(homePage.LastIndexOf('/') + 1); if (addr.Contains("广州") && !string.IsNullOrWhiteSpace(userCode)) { FateUserInfo user = FateUserInfoManager.GetUser(userCode); if (user == null) { user = new FateUserInfo() { CreateTime = DateTime.Now }; } user.ModifyTime = DateTime.Now; user.UserCode = userCode; user.Address = addr; user.Age = age; user.HeadFileName = pic; user.UserName = userName; FateUserInfoManager.SaveOrUpdateUser(user); } } } }
/// <summary> /// 解析H3中的文本链接 /// </summary> private string GetContentLink(NSoup.Nodes.Element el) { string content = el.Text(); string link = string.Empty; if (!string.IsNullOrWhiteSpace(content)) { List <string> contents = Regex.Split(content, ":|:").ToList(); if (contents != null && contents.Count > 1) { contents.RemoveAt(0); link = string.Join(":", contents); } } return(link); }
public static string GetText(NSoup.Nodes.Element el) { return(el == null ? null : el.Text()); }