public static void GetQuestionInfoFormHtml(string HtmlStr) { if (!String.IsNullOrEmpty(HtmlStr)) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(HtmlStr); HtmlNodeCollection questionItems = doc.DocumentNode.SelectNodes(@"//div[@class='feed-item feed-item-hook question-item']"); if (questionItems != null && questionItems.Count > 0) { foreach (HtmlNode node in questionItems) { try { HtmlNode subTopic = node.SelectNodes(@"div[@class='subtopic']//a").ToList()[0]; HtmlNode titleNode = node.SelectNodes(@"h2[@class='question-item-title']").ToList()[0]; string subtopicId = ""; string timeStamp = ""; string title = ""; string questionId = ""; subtopicId = subTopic.Attributes.FirstOrDefault(p => p.Name == "href").Value; string[] topic = subtopicId.Split('/'); subtopicId = topic[2]; timeStamp = titleNode.SelectNodes(@"span").ToList()[0].Attributes.FirstOrDefault(p => p.Name == "data-timestamp").Value; questionId = titleNode.SelectNodes(@"a").ToList()[0].Attributes.FirstOrDefault(p => p.Name == "href").Value; title = titleNode.SelectNodes(@"a").ToList()[0].InnerText; string[] question = questionId.Split('/'); foreach (var j in question) { questionId = question[2]; } QuestionInfo qi = new QuestionInfo(); qi.QuestionID = int.Parse(questionId); qi.QuestionTitle = title; qi.QuestionTimeStamp = long.Parse(timeStamp); qi.BelongsTopic = subtopicId; qi.ModefiedTime = DateTime.Now.ToString(); qi.QuestionUrl = @"http://www.zhihu.com/question/" + qi.QuestionID; if (QuestionInfoDB.SaveOrUpdateQuestionInfo(qi)) { Console.WriteLine("问题:" + qi.QuestionID + " " + qi.QuestionTitle + " 于 " + qi.ModefiedTime + " 保存完成"); } else { Console.WriteLine("问题:" + qi.QuestionID + " " + qi.QuestionTitle + " 于 " + qi.ModefiedTime + " 保存失败"); } } catch (Exception ex) { } } DownLoadCount += 1; if (DownLoadCount % 10 == 0) { SysDictDB.SaveOrUpdateDownLoadPageCount(DownLoadCount); } Thread.Sleep(1000 * 5); } } }
public static bool DeleteQuestionInfo(QuestionInfo qi) { bool result = false; try { if (qi != null) { using (MainDataContext db = new MainDataContext()) { QuestionInfo Info = db.QuestionInfos.FirstOrDefault(p => p.QuestionID == qi.QuestionID); if (Info != null) { db.QuestionInfos.DeleteOnSubmit(Info); } db.SubmitChanges(); result = true; } } } catch (Exception ex) { } return result; }
partial void DeleteQuestionInfo(QuestionInfo instance);
partial void UpdateQuestionInfo(QuestionInfo instance);
partial void InsertQuestionInfo(QuestionInfo instance);
public static bool SaveOrUpdateQuestionInfo(QuestionInfo qi) { bool result = false; try { using (MainDataContext db = new MainDataContext()) { QuestionInfo Info = db.QuestionInfos.FirstOrDefault(p => p.QuestionID == qi.QuestionID); if (Info == null) { db.QuestionInfos.InsertOnSubmit(qi); } else { Info = qi; } db.SubmitChanges(); result = true; } } catch (Exception ex) { } return result; }