def unfollow_member(self): self.parse() username = self.get_title().split(",")[0].strip() #中文的逗号 #unfollow_button = self.soup.find("button", class_="zg-btn zg-btn-unfollow zm-rich-follow-btn with-icon") follow_button = self.soup.find( "button", class_="Button FollowButton Button--primary Button--blue") unfollow_button = self.soup.find( "button", class_="Button FollowButton Button--primary Button--grey") if not unfollow_button and follow_button: print termcolor.colored("您还没有关注用户{0}".format(username), "red") return data_id = str(unfollow_button.get("data-id")) url = "https://www.zhihu.com/node/MemberFollowBaseV2" params = {"hash_id": data_id} data = { "method": "unfollow_member", "params": json.dumps(params), "_xsrf": self._xsrf } #res = session.post(url, data=data, headers=self.headers) res = mul_post_request(session, url, headers, data=data) if not res: return if not res.json()["r"]: print termcolor.colored("取消关注用户{}成功".format(username), "blue")
def follow_member(self): self.parse() username = self.get_name() #follow_button = self.soup.find("button", class_="zg-btn zg-btn-follow zm-rich-follow-btn with-icon") follow_button = self.soup.find("button", class_="zg-btn zg-btn-follow zm-rich-follow-btn") unfollow_button = self.soup.find("button", class_="zg-btn zg-btn-unfollow zm-rich-follow-btn") if not follow_button and unfollow_button: print termcolor.colored("您已经关注了用户{}".format(username), "red") return data_id = str(follow_button.get("data-id")) url = "https://www.zhihu.com/node/MemberFollowBaseV2" params = { "hash_id": data_id } data = { "method": "follow_member", "params": json.dumps(params), "_xsrf": self._xsrf } #res = session.post(url, data=data, headers=self.headers) res = mul_post_request(session, url, headers, data=data) if not res: return if not res.json()["r"]: print termcolor.colored("关注用户{}成功".format(username), "blue")
def unfollow_member(self): self.parse() username = self.get_title().split(",")[0].strip() #中文的逗号 #unfollow_button = self.soup.find("button", class_="zg-btn zg-btn-unfollow zm-rich-follow-btn with-icon") follow_button = self.soup.find("button", class_="Button FollowButton Button--primary Button--blue") unfollow_button = self.soup.find("button", class_="Button FollowButton Button--primary Button--grey") if not unfollow_button and follow_button: print termcolor.colored("您还没有关注用户{0}".format(username), "red") return data_id = str(unfollow_button.get("data-id")) url = "https://www.zhihu.com/node/MemberFollowBaseV2" params = { "hash_id": data_id } data = { "method": "unfollow_member", "params": json.dumps(params), "_xsrf": self._xsrf } #res = session.post(url, data=data, headers=self.headers) res = mul_post_request(session, url, headers, data=data) if not res: return if not res.json()["r"]: print termcolor.colored("取消关注用户{}成功".format(username), "blue")
def vote_down(self): url = "https://www.zhihu.com/node/AnswerVoteBarV2" params = {"answer_id": self.get_data_id()} data = { "method": "vote_down", "_xsrf": self._xsrf, "params": json.dumps(params) } #res = session.post(url, data) res = mul_post_request(session, url, headers, data=data) if not res: return #print res.content if not res.json()["r"]: print termcolor.colored("反对成功", "blue")
def get_answers(self): self.check() answer_num = self.get_answer_num() if not answer_num: print termcolor.colored("该问题还没有回答", "magenta") return items = self.soup.find_all("div", class_="zm-item-answer zm-item-expanded") answer_wrap = self.soup.find("div", id="zh-question-answer-wrap") summarys = answer_wrap.find_all("div", class_="zh-summary summary clearfix") #mode = re.compile(r"^zm-item-vote-info") #items = sorted(items, lambda x, y: int(y.find("div", class_=mode).get("data-votecount")) - int(x.find("div", class_=mode).get("data-votecount"))) count = 0 iter = 0 times = 0 for item in items: count += 1 summary = summarys[iter] iter += 1 qaitem = QAItem(item, self._xsrf, summary) yield qaitem while count <= answer_num: url_token = re.findall(r"(\d+)", self.url)[0] url = "https://www.zhihu.com/node/QuestionAnswerListV2" pagesize = 10 offset = pagesize * times + 10 params = { "url_token": url_token, "pagesize": pagesize, "offset": offset } data = { "method": "next", "params": json.dumps(params), "_xsrf": self._xsrf } #res = session.post(url, data, headers=self.headers) res = mul_post_request(session, url, headers, data=data) if not res: return #print res.content items = res.json()["msg"] #print items for item in items: count += 1 iitem = BeautifulSoup(item, "html.parser") qaitem = QAItem(iitem, self._xsrf) yield qaitem times += 1
def vote_cancle_answer(self): url = "https://www.zhihu.com/node/AnswerVoteBarV2" params = { "answer_id": self.get_data_id() } data = { "method": "vote_neutral", "_xsrf": self._xsrf, "params": json.dumps(params) } #res = session.post(url, data, headers=self.headers) res = mul_post_request(session, url, headers, data=data) if not res: return #print res.content if res.json()["r"] == 0: print termcolor.colored("取消赞同成功", "blue")
def follow_question(self): self.parse() button = self.soup.find("button", class_="follow-button zg-follow zg-btn-green") if not button: print termcolor.colored("您已经关注了问题:{}".format(self.get_title()), "red") return url = "https://www.zhihu.com/node/QuestionFollowBaseV2" params = { "question_id": self.get_data_resourceid() } data = { "method": "follow_question", "_xsrf": self._xsrf, "params": json.dumps(params) } #res = session.post(url, data, headers=self.headers) res = mul_post_request(session, url, headers, data=data) if not res: return if res.json()["r"] == 0: print termcolor.colored("关注问题:{}成功".format(self.get_title()), "blue")
def add_to_collections(self): ''' 暂不支持创建新的收藏夹 ''' url = "https://www.zhihu.com/collections/json?answer_id={}".format(self.get_data_id()) #res = session.get(url, headers=self.headers) res = mul_get_request(session, url, headers) if not res: return collections = res.json()["msg"][0] print "\n", for i, m in enumerate(collections): print "{0}:{1} {2}条答案 {3}人关注".format(i, m[1], m[3], m[4]) print "\n", index = -1 length = len(collections) while not (0 <= index < length): num = raw_input("请选择收藏夹序号(0-{}), 输入cancle取消操作\n".format(length-1)) if num == "cancle": return try: index = int(num) except: print termcolor.colored("请输入正确的序号", "red") add_url = "https://www.zhihu.com/collection/add" data = { "answer_id": self.get_data_id(), "_xsrf": self._xsrf, "favlist_id": str(collections[index][0]) } #res = session.post(add_url, data, headers=self.headers) res = mul_post_request(session, add_url, self.headers, data=data) if not res: return print "\n", if res.json()["r"] == 0: print termcolor.colored("收藏成功", "green") else: print termcolor.colored("您已经收藏过该答案", "green")