Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question.get_all_answers()的Bug更正后可以搜集全所有的回答 #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions zhihu.py
Original file line number Diff line number Diff line change
@@ -371,15 +371,16 @@ def get_all_answers(self):
else:
error_answer_count = 0
my_answer_count = 0
for i in xrange((answers_num - 1) / 20 + 1):
for i in xrange((answers_num - 1) / 10 + 1):
if i == 0:
for j in xrange(min(answers_num, 20)):
for j in xrange(min(answers_num, 10)):
if self.soup == None:
self.parser()
soup = BeautifulSoup(self.soup.encode("utf-8"), "lxml")

is_my_answer = False
if soup.find_all("div", class_="zm-item-answer")[j].find("span", class_="count") == None:
#print len(soup.find_all("div", class_="zm-item-answer"))
if soup.find_all("div", class_="zm-item-answer zm-item-expanded")[j].find("span", class_="count") == None:
my_answer_count += 1
is_my_answer = True

@@ -428,9 +429,9 @@ def get_all_answers(self):
else:
post_url = "http://www.zhihu.com/node/QuestionAnswerListV2"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方没看懂哦,是知乎的js接口变了吗?现在用fidder4抓的JS地址是api/questions/37120133/answers?sort_by-default&include-data%5b.....等等等等,后面好长一串参数

_xsrf = self.soup.find("input", attrs={'name': '_xsrf'})["value"]
offset = i * 20
offset = i * 10
params = json.dumps(
{"url_token": int(self.url[-8:-1] + self.url[-1]), "pagesize": 20, "offset": offset})
{"url_token": int(self.url[-8:]), "pagesize": 10, "offset": offset})
data = {
'_xsrf': _xsrf,
'method': "next",
@@ -444,7 +445,7 @@ def get_all_answers(self):
r = requests.post(post_url, data=data, headers=header, verify=False)

answer_list = r.json()["msg"]
for j in xrange(min(answers_num - i * 20, 20)):
for j in xrange(min(answers_num - i * 10, 10)):
soup = BeautifulSoup(self.soup.encode("utf-8"), "lxml")

answer_soup = BeautifulSoup(answer_list[j], "lxml")