/** * 获取该话题下精华回答 * @return Generator 精华回答列表 */ public function top_answer() { $top_answer_url = $this->url . TOPICS_TOP_SUFFIX_URL; $r = Request::get($top_answer_url); $dom = str_get_html($r); $max_page = (int) $dom->find('div.zm-invite-pager span', -2)->plaintext; for ($i = 1; $i <= $max_page; $i++) { if ($i > 1) { $page_url = $top_answer_url . GET_PAGE_SUFFIX_URL . $i; $r = Request::get($page_url); $dom = str_get_html($r); } for ($j = 0; !empty($item = $dom->find('div.feed-item', $j)); $j++) { $answer_url = ZHIHU_URL . $item->find('div.zm-item-rich-text', 0)->attr['data-entry-url']; $question_link = $item->find('h2 a.question_link', 0); $question = parser_question($question_link); $author_link = $item->find('div.zm-item-answer-author-info a', 0); $author = parser_user_from_topic($author_link); $upvote = $item->find('span.count', 0)->plaintext; $content = $item->find('textarea.content', 0)->plaintext; (yield new Answer($answer_url, $question, $author, $upvote, $content)); } } }
/** * 获取用户回答列表 * @return Generator 回答列表生成器 */ public function answers() { if (empty($this->url)) { (yield null); } else { $answers_num = $this->answers_num(); if ($answers_num == 0) { (yield null); } else { for ($i = 0; $i < $answers_num / 20; $i++) { $answer_page_url = $this->url . ANSWERS_PAGE_SUFFIX_URL . ($i + 1); $r = Request::get($answer_page_url); $dom = str_get_html($r); for ($j = 0; $j < min($answers_num - $i * 20, 20); $j++) { $question_link = $dom->find('a.question_link', $j); $answer_url = ZHIHU_URL . $question_link->href; $question = parser_question($question_link); (yield new Answer($answer_url, $question)); } } } } }