Пример #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            var oldIds = from t in db.QuestionSet select t.Id;
            var oldAnswerIds = from t in db.AnswerSet select t.Id;

            List<decimal> newIds = new List<decimal>();
            List<decimal> newAnswerIds = new List<decimal>();
            //WebBrowser browser = new WebBrowser();
            browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
            browserDetail.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browserDetail_DocumentCompleted);

            var pageCount = (int)numericUpDown1.Value;
            
            for (int i = pageCount; i > 0; i--)
            {
                var url = string.Format("http://www.zhihu.com/topic/19776749/top-answers?page={0}", i);
                
                //NetworkCredential c = new NetworkCredential("*****@*****.**","password");
                //HtmlAgilityPack.HtmlDocument doc =  htmlWeb.Load(url,"get",null,c);

                browser.Navigate(url);
                while (!ready)
                {
                    Application.DoEvents();
                }
                //System.Threading.Thread.Sleep(1000);
                ready = false;
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(browser.Document.Body.OuterHtml);

                var nodes = Utility.FindNodesByClass(doc, "feed-main");
                foreach (var node in nodes)
                {
                    //先找问题
                    Question q = new Question();
                    q.Id = Utility.GetTopId(node);
                    //q.AnswerCount = GetAnswerCount(node); 拿不到,html里面没有
                    //q.CreateTime = Utility.GetTopTime(node);
                    //q.FollowerCount = GetFollower(node);
                    q.Title = Utility.GetTopTitle(node);
                    q.Topic = Utility.GetTopic(node);
                    //q.ViewCount = GetViewCount(node);

                    bool contain = newIds.Contains(q.Id) || oldIds.Contains(q.Id);

                    if (!contain)
                    {
                        db.QuestionSet.AddObject(q);
                        newIds.Add(q.Id);

                        // Application.DoEvents();
                    }
                    Application.DoEvents();
                    txtTitle.Text = string.Format("P:{0},T:{1}", i, q.Title);

                    //再找答案
                    var answerId = Utility.GetAnswerId(node);
                    Answer a;
                    bool containAnswer = newAnswerIds.Contains(answerId) || oldAnswerIds.Contains(answerId);
                    if (!containAnswer)
                    {
                        a = new Answer();
                        a.Id = answerId;
                        db.AnswerSet.AddObject(a);
                        newIds.Add(a.Id);
                        //Application.DoEvents();
                    }
                    else
                    {
                        a = db.AnswerSet.First(t => t.Id == answerId);
                    }

                    a.Voteup = Utility.GetVoteCount(node);
                    a.Author = Utility.GetAnswerAuthor(node);
                    a.AuthorId = Utility.GetAnswerAuthorId(node);
                   
                    var answerUrl = string.Format("http://www.zhihu.com/question/{0}/answer/{1}", q.Id, a.Id);

                    
                    //var answerDoc = htmlWeb.Load(answerUrl);
                    browserDetail.Navigate(answerUrl);
                    while (!readyDetail)
                    {
                        Application.DoEvents();
                    }
                    readyDetail = false;
                    HtmlAgilityPack.HtmlDocument answerDoc = new HtmlAgilityPack.HtmlDocument();
                    answerDoc.LoadHtml(browserDetail.Document.Body.OuterHtml);

                    var rootNode = answerDoc.DocumentNode;
                    //var author = Utility.GetAnswerAuthor2(rootNode);
                    //if ( string.IsNullOrEmpty(a.Author) && !string.IsNullOrEmpty(author))
                    //{
                    //    a.Author = author;
                    //}
                    a.CollectCount = Utility.GetCollectCount(rootNode);
                    a.CreateTime = Utility.GetAnswerTime(rootNode);
                    a.CommentCount = Utility.GetCommentCount(rootNode);
                    a.QuestionId = q.Id;
                }

                db.SaveChanges();

            }
            MessageBox.Show("Over!");

        }
Пример #2
0
 /// <summary>
 /// Create a new Answer object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="voteup">Initial value of the Voteup property.</param>
 /// <param name="collectCount">Initial value of the CollectCount property.</param>
 /// <param name="commentCount">Initial value of the CommentCount property.</param>
 /// <param name="questionId">Initial value of the QuestionId property.</param>
 public static Answer CreateAnswer(global::System.Decimal id, global::System.Decimal voteup, global::System.Decimal collectCount, global::System.Decimal commentCount, global::System.Decimal questionId)
 {
     Answer answer = new Answer();
     answer.Id = id;
     answer.Voteup = voteup;
     answer.CollectCount = collectCount;
     answer.CommentCount = commentCount;
     answer.QuestionId = questionId;
     return answer;
 }
Пример #3
0
 /// <summary>
 /// Deprecated Method for adding a new object to the AnswerSet EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToAnswerSet(Answer answer)
 {
     base.AddObject("AnswerSet", answer);
 }
Пример #4
0
 /// <summary>
 /// Deprecated Method for adding a new object to the AnswerSet EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToAnswerSet(Answer answer)
 {
     base.AddObject("AnswerSet", answer);
 }
Пример #5
0
        private void button2_Click(object sender, EventArgs e)
        {
            var oldIds       = from t in db.QuestionSet select t.Id;
            var oldAnswerIds = from t in db.AnswerSet select t.Id;

            List <decimal> newIds       = new List <decimal>();
            List <decimal> newAnswerIds = new List <decimal>();

            //WebBrowser browser = new WebBrowser();
            browser.DocumentCompleted       += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
            browserDetail.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browserDetail_DocumentCompleted);

            var pageCount = (int)numericUpDown1.Value;

            for (int i = pageCount; i > 0; i--)
            {
                var url = string.Format("http://www.zhihu.com/topic/19776749/top-answers?page={0}", i);

                //NetworkCredential c = new NetworkCredential("*****@*****.**","password");
                //HtmlAgilityPack.HtmlDocument doc =  htmlWeb.Load(url,"get",null,c);

                browser.Navigate(url);
                while (!ready)
                {
                    Application.DoEvents();
                }
                //System.Threading.Thread.Sleep(1000);
                ready = false;
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(browser.Document.Body.OuterHtml);

                var nodes = Utility.FindNodesByClass(doc, "feed-main");
                foreach (var node in nodes)
                {
                    //先找问题
                    Question q = new Question();
                    q.Id = Utility.GetTopId(node);
                    //q.AnswerCount = GetAnswerCount(node); 拿不到,html里面没有
                    //q.CreateTime = Utility.GetTopTime(node);
                    //q.FollowerCount = GetFollower(node);
                    q.Title = Utility.GetTopTitle(node);
                    q.Topic = Utility.GetTopic(node);
                    //q.ViewCount = GetViewCount(node);

                    bool contain = newIds.Contains(q.Id) || oldIds.Contains(q.Id);

                    if (!contain)
                    {
                        db.QuestionSet.AddObject(q);
                        newIds.Add(q.Id);

                        // Application.DoEvents();
                    }
                    Application.DoEvents();
                    txtTitle.Text = string.Format("P:{0},T:{1}", i, q.Title);

                    //再找答案
                    var    answerId = Utility.GetAnswerId(node);
                    Answer a;
                    bool   containAnswer = newAnswerIds.Contains(answerId) || oldAnswerIds.Contains(answerId);
                    if (!containAnswer)
                    {
                        a    = new Answer();
                        a.Id = answerId;
                        db.AnswerSet.AddObject(a);
                        newIds.Add(a.Id);
                        //Application.DoEvents();
                    }
                    else
                    {
                        a = db.AnswerSet.First(t => t.Id == answerId);
                    }

                    a.Voteup   = Utility.GetVoteCount(node);
                    a.Author   = Utility.GetAnswerAuthor(node);
                    a.AuthorId = Utility.GetAnswerAuthorId(node);

                    var answerUrl = string.Format("http://www.zhihu.com/question/{0}/answer/{1}", q.Id, a.Id);


                    //var answerDoc = htmlWeb.Load(answerUrl);
                    browserDetail.Navigate(answerUrl);
                    while (!readyDetail)
                    {
                        Application.DoEvents();
                    }
                    readyDetail = false;
                    HtmlAgilityPack.HtmlDocument answerDoc = new HtmlAgilityPack.HtmlDocument();
                    answerDoc.LoadHtml(browserDetail.Document.Body.OuterHtml);

                    var rootNode = answerDoc.DocumentNode;
                    //var author = Utility.GetAnswerAuthor2(rootNode);
                    //if ( string.IsNullOrEmpty(a.Author) && !string.IsNullOrEmpty(author))
                    //{
                    //    a.Author = author;
                    //}
                    a.CollectCount = Utility.GetCollectCount(rootNode);
                    a.CreateTime   = Utility.GetAnswerTime(rootNode);
                    a.CommentCount = Utility.GetCommentCount(rootNode);
                    a.QuestionId   = q.Id;
                }

                db.SaveChanges();
            }
            MessageBox.Show("Over!");
        }