/** * 获取收藏该回答的收藏夹列表 * @return Generator 收藏夹列表 */ public function collections() { $collections_num = $this->collections_num(); if ($collections_num == 0) { (yield null); } else { $collection_url = $this->url . COLLECTION_SUFFIX_URL; $r = Request::get($collection_url); $dom = str_get_html($r); $_xsrf = _xsrf($dom); $json = $dom->find('div.zh-general-list', 0)->attr['data-init']; for ($i = 0; $i < $collections_num / 20; $i++) { if ($i == 0) { for ($j = 0; $j < min($collections_num, 20); $j++) { $collection_link = $dom->find('div.zm-item', $j); (yield parser_collection_from_answer($collection_link)); } } else { $post_url = COLLECTION_LIST_URL; $params = json_decode(html_entity_decode($json))->params; $params->offset = $i * 20; $params = json_encode($params); $data = array('method' => 'next', 'params' => $params, '_xsrf' => $_xsrf); $r = Request::post($post_url, $data, array("Referer: {$collection_url}")); $r = json_decode($r)->msg; for ($j = 0; $j < count($r); $j++) { $dom = str_get_html($r[$j]); if (!empty($dom)) { $collection_link = $dom->find('div.zm-item', 0); (yield parser_collection_from_answer($collection_link)); } } } } } }
/** * 获取问题关注者 * @return Generator 关注者列表迭代器 */ public function followers() { $followers_num = $this->followers_num(); if ($followers_num == 0) { (yield null); } else { $follwers_url = $this->url . FOLLOWERS_SUFFIX_URL; $r = Request::get($follwers_url); $dom = str_get_html($r); $_xsrf = _xsrf($dom); for ($i = 0; $i < $followers_num / 20; $i++) { if ($i == 0) { for ($j = 0; $j < min($followers_num, 20); $j++) { $follower_link = $dom->find('div.zm-profile-card h2 a', $j); (yield parser_user($follower_link)); } } else { $data = array('start' => 0, 'offset' => $i * 20, '_xsrf' => $_xsrf); $r = Request::post($follwers_url, $data, array("Referer: {$follwers_url}")); $r = json_decode($r)->msg; $dom = str_get_html($r[1]); for ($j = 0; $j < min($followers_num - $i * 20, 20); $j++) { $follower_link = $dom->find('div.zm-profile-card h2', $j); (yield parser_user($follower_link)); } } } } }
/** * 获取问题列表 * @param string $url 目标 url * @return Generator 问题迭代器 */ private function question($url) { $r = Request::get($url); $dom = str_get_html($r); $_xsrf = _xsrf($dom); $tmp_url = null; for ($i = 0; !empty($combine = $dom->find('div.feed-item', $i)); $i++) { $offset = $combine->attr['data-score']; $question_link = $combine->find('h2 a.question_link', 0); $question_url = ZHIHU_URL . $question_link->href; if ($question_url != $tmp_url) { $tmp_url = $question_url; $question_title = $question_link->plaintext; (yield new Question($question_url, $question_title)); } } do { $data = array('start' => 0, 'offset' => $offset, '_xsrf' => $_xsrf); $r = Request::post($url, $data, array('Referer: {$url}"')); $r = json_decode($r)->msg; $item = $r[0]; $dom = str_get_html($r[1]); for ($i = 0; $item && !empty($combine = $dom->find('div.feed-item', $i)); $i++) { $offset = $combine->attr['data-score']; $question_link = $combine->find('h2 a.question_link', 0); $question_url = ZHIHU_URL . $question_link->href; if ($question_url != $tmp_url) { $tmp_url = $question_url; $question_title = $question_link->plaintext; (yield new Question($question_url, $question_title)); } } } while ($item); }
/** * 获取用户列表 * @param String $type 关注或者粉丝 * @return Generator 用户列表 */ private function follow($type) { if ($type === 'FOLLOWERS') { $num = $this->followers_num(); $url = $this->url . FOLLOWERS_SUFFIX_URL; $post_url = FOLLOWERS_LIST_URL; } elseif ($type === 'FOLLOWEES') { $num = $this->followees_num(); $url = $this->url . FOLLOWEES_SUFFIX_URL; $post_url = FOLLOWEES_LIST_URL; } if ($num <= 0) { return null; } else { $r = Request::get($url); $dom = str_get_html($r); $_xsrf = _xsrf($dom); $json = $dom->find('div.zh-general-list', 0)->attr['data-init']; for ($i = 0; $i < $num / 20; $i++) { if ($i == 0) { for ($j = 0; $j < min($num, 20); $j++) { $user_list = $dom->find('h2.zm-list-content-title', $j); (yield parser_user($user_list)); } } else { $params = json_decode(html_entity_decode($json))->params; $params->offset = $i * 20; $params = json_encode($params); $data = array('_xsrf' => $_xsrf, 'method' => 'next', 'params' => $params); $r = Request::post($post_url, $data, array("Referer: {$url}")); $r = json_decode($r)->msg; for ($j = 0; $j < min($num - $i * 20, 20); $j++) { $dom = str_get_html($r[$j]); $user_list = $dom->find('h2.zm-list-content-title', 0); (yield parser_user($user_list)); } } } } }