Example #1
0
def testall():
    url = "http://www.zhihu.com/question/24269892"
    question = Question(url)
    answers = question.get_all_answers()
    for answer in answers:
        answer.to_txt()
        answer.to_md()
Example #2
0
def testall():
    url = "http://www.zhihu.com/question/24269892"
    question = Question(url)
    answers = question.get_all_answers()
    for answer in answers:
        answer.to_txt()
        answer.to_md()
Example #3
0
def test():
    url = "http://www.zhihu.com/question/24269892"
    question = Question(url)
    # 得到排名第一的答案
    answer = question.get_top_answer()
    # 得到排名第一的答案的作者
    user = answer.get_author()
    # 得到该作者回答过的所有问题的答案
    user_answers = user.get_answers()
    # 输出该作者回答过的所有问题的标题
    for answer in user_answers:
        print 'answer_url',answer.answer_url
        print answer.get_question()
        print answer.get_question().get_title()
    # 得到该用户的所有收藏夹
    user_collections = user.get_collections()
    for collection in user_collections:
        # 输出每一个收藏夹的名字
        print collection.get_name()
        # 得到该收藏夹下的前十个回答
        top_answers = collection.get_top_i_answers(10)
        # 把答案内容转成txt,markdown
        for answer in top_answers:
            answer.to_txt()
            answer.to_md()
Example #4
0
def main():
	url = "https://www.zhihu.com/question/40935515"
	question = Question(url)
	answers = question.get_all_answers()
	i = 1
	for answer in answers:
		print i
		i = i+1
def dealWithPage(page_num):
    page = urlparse.urljoin(url_prefix, str(page_num))

    question_index_filename = 'q_index'
    try:
        question = Question(page)
    except:
        print 'Question Get Error.'
        return
    f = codecs.open(question_index_filename + '.txt', 'a')
    f.write(str(page_num) + '\r\n')
    f.close()
    question_folder = 'Zhihu/Question_' + str(page_num)
    question_filename = 'q'
    if (not os.path.exists(question_folder)):
        os.makedirs(question_folder)
    f = codecs.open(os.path.join(question_folder, question_filename + '.txt'),
                    'w')
    f.write(question.get_title() + '\r\n|||\r\n')
    f.write(question.get_detail() + '\r\n|||\r\n')
    for topic in question.get_topics():
        f.write(topic + '\t')
    f.write('\r\n|||\r\n')
    f.write(str(question.get_visit_times()) + '\r\n|||\r\n')
    f.write(str(question.get_followers_num()) + '\r\n|||\r\n')
    f.write(str(question.get_answers_num()) + '\r\n|||\r\n')
    f.close()

    answers = question.get_all_answers()
    for answer in answers:
        ansURL = answer.answer_url
        ans = Answer(ansURL)
        answer_folder = os.path.join(question_folder, 'Answer')
        answer_filename = str(ansURL.split('/')[-1])
        answer_index_filename = 'a_index'
        f = codecs.open(answer_index_filename + '.txt', 'a')
        f.write(str(page_num) + '|||' + str(answer_filename) + '\r\n')
        f.close()
        if (not os.path.exists(answer_folder)):
            os.makedirs(answer_folder)
        f = codecs.open(os.path.join(answer_folder, answer_filename + '.txt'),
                        'w')
        try:
            f.write(ans.get_content().find('body').get_text().strip().encode(
                "gbk", 'ignore') + '\r\n|||\r\n')
            f.write(ans.get_author().get_user_id() + '\r\n|||\r\n')
            f.write(str(ans.get_upvote()) + '\r\n|||\r\n')
        except:
            print 'TimeOut Occurred.'
            f.close()
            f = codecs.open(
                os.path.join(answer_folder, answer_filename + '.txt'), 'w')
            f.write('None')
            f.close()
            continue
        f.close()
    def setUpClass(cls):
        url = 'http://www.zhihu.com/question/24825703'
        file_path = os.path.join(TEST_DATA_PATH, 'question.html')
        with open(file_path, 'rb') as f:
            html = f.read()
        soup = BeautifulSoup(html)

        cls.question = Question(url)
        cls.question._session = None
        cls.question.soup = soup
        cls.expected = {
            'id': 24825703,
            'qid': 2112271,
            'xsrf': 'cfd489623d34ca03adfdc125368c6426',
            'html': soup.prettify(),
            'title': '关系亲密的人之间要说「谢谢」吗?',
            'details': description,
            'answer_num': 621,
            'follower_num': 4427,
            'top_answer_id': 39753456,
            'top_answer_author_name': '芝士就是力量',
            'top_answer_upvote_num': 97,
            'top_50_ans_id': 31003847,
            'top_50_ans_author_name': '圭多达莱佐',
            'top_50_ans_upvote_num': 31,
            'more_ans_id': 39958704,
            'more_ans_author_name': '柳蜻蜓',
            'more_ans_upvote_num': 1,
            'topics': ['心理学', '恋爱', '社会', '礼仪', '亲密关系'],
        }

        more_ans_file_path = os.path.join(TEST_DATA_PATH,
                                          'question_more_answer.html')
        with open(more_ans_file_path, 'rb') as f:
            cls.more_ans_html = f.read()
Example #7
0
def exp_question():
    q = Question('http://www.zhihu.com/question/29693016')

    # 获取问题标签
    for tag in q.get_tags:
        print tag,
    # 校园招聘 实习生 社会招聘 面试技巧 互联网求职


    # 获得问题标题
    print q.get_question
    # 互联网求职路上,你见过哪些写得很好、很用心的面经?最好能分享自己的面经、心路历程。


    # 获取问题描述
    """以markdown形式输出"""
    print q.ques_description
    # 技术类、非技术类的都行;校招、社招的都行。诚然,此问题并不局限于分享面经,也可以授之以渔而非授之以鱼。...


    # 获取问题关注者人数
    print q.ques_followers
    # 关注者人数为1323


    # 获取问题回答数
    print q.answer_num
    # 回答人数为:17


    # 获取排在最前面的回答
    print q.top_answer
    # 第1个答案:
    # 自问自答。开这个问题的初衷是希望知乎上的CS大牛们能分享一下自己的求职心路历程,既然大家都这么羞涩,我只好抛砖引玉(一般来说,需要二次跳转查看的答案传播面都不会太广)。


    # 获取排名前几位的回答
    print q.top_i_answers(2)
    # ...
    # 第2个答案:
    # [@Michael282694](http://www.zhihu.com/people/7303e4f770e8055f3bedc4cf3b192325)说我不回答就要拉黑我,心理感觉到了深深的恐惧(她说她准备了5把刀)
    # ...


    # 获取所有回答
    print q.all_answers
Example #8
0
    def question_test(self, url):
        question = Question(url)

        # 获取该问题的标题
        title = question.get_title()
        # 获取该问题的详细描述
        detail = question.get_detail()
        # 获取回答个数
        # answers_num = question.get_answers_num()
        # 获取关注该问题的人数
        # followers_num = question.get_followers_num()
        # 获取该问题所属话题
        topics = question.get_topics()
        # 获取该问题被浏览次数
        # visit_times = question.get_visit_times()
        # 获取排名第一的回答
        # top_answer = question.get_top_answer()
        # 获取排名前十的十个回答
        top_answers = question.get_top_i_answers(10)
        # 获取所有回答
        answers = question.get_all_answers()

        # print title  # 输出:现实可以有多美好?
        # print detail
        # 输出:
        # 本问题相对于“现实可以多残酷?传送门:现实可以有多残酷?
        # 题主:       昨天看了“现实可以有多残酷“。感觉不太好,所以我
        # 开了这个问题以相对应,希望能够“中和一下“。和那个问题题主不想
        # 把它变成“比惨大会“一样,我也不想把这个变成“鸡汤故事会“,或者
        # 是“晒幸福“比赛。所以大家从“现实,实际”的角度出发,讲述自己的
        # 美好故事,让大家看看社会的冷和暖,能更加辨证地看待世界,是此
        # 题和彼题共同的“心愿“吧。
        # print answers_num  # 输出:2441
        # print followers_num  # 输出:26910
        # for topic in topics:
        # print type(topic)
        # print topic,  # 输出:情感克制 现实 社会 个人经历
        # print visit_times  # 输出: 该问题当前被浏览的次数
        # print top_answer  # 输出:<zhihu.Answer instance at 0x7f8b6582d0e0>(Answer类对象)
        # print top_answers  # 输出:<generator object get_top_i_answers at 0x7fed676eb320>(代表前十的Answer的生成器)
        # print answers  # 输出:<generator object get_all_answer at 0x7f8b66ba30a0>(代表所有Answer的生成器)
        answers_list = []
        for answer in answers:  #top_answers
            answers_list.append(answer.to_txt())
        if len(answers_list) > 0:
            answers = []
            topics = json.dumps(topics).decode('unicode-escape').encode('utf8')
            time_now = int(time.time())
            p_str = 'INSERT IGNORE INTO ANSWER (QURL, TITLE, DETAIL, TOPICS, AURL, USERID, CONTENT, UPVOTE, USERURL, ADD_TIME, LAST_VISIT) VALUES (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s,%s)'
            for answer in answers_list:
                answers = answers + [
                    (url, title, detail, topics, answer["aurl"],
                     answer["userid"], answer["content"], answer["upvote"],
                     answer["userurl"], time_now, 0)
                ]
            self.cursor.executemany(p_str, answers)
            return self.cursor.rowcount
        else:
            print "no answer"
            return 0
Example #9
0
 def test_follow_question_with_answer_url(self):
     """
     也支持回答的URL,因为从回答中也能找到问题的ID
     :return:
     """
     data = Question(
         url='https://www.zhihu.com/question/59001738/answer/160832685'
     ).follow_question()
     self.assertEqual({"is_following": True}, data)
Example #10
0
File: work.py Project: bo5509/zhihu
    def question(self, url):
        question = Question(url)

        title = question.get_title()  # 标题
        detail = question.get_detail()  # 描述
        answers_num = question.get_answers_num()  # 回答个数
        followers_num = question.get_followers_num()  # 关注人数
        topics = question.get_topics()  # 所属话题
        top_answers = question.get_top_i_answers(10)  # 前十回答

        return title, detail, answers_num, followers_num, topics, top_answers
Example #11
0
def main():
	urls = [
	"https://www.zhihu.com/topic/19843695"
	]
	topicquestion = Topicquestion(urls)
	topicquestion.execute()
	
	f = open("config/question.list","r")

	lines = f.readlines()
	for line in lines: 
		url = "https://www.zhihu.com/question/"+line.strip()
		question = Question(url)
		answers = question.get_all_answers()
		once = True
		for answer in answers:    
			answer.to_sql(line.strip()+'.sql',once,question.get_detail())
			once = False
	f.close()
def main():
    vl = []
    viplist = []
    qlist = []
    fi=open("userlist.txt")
    msg = fi.readline()
    while msg :
        name = msg[0:len(msg)-1]
        vl.append(name)
        msg=fi.readline()
    fi.close()
    
    f1=open("susp/list2.txt")
    msg = f1.readline()
    while msg :
        name = msg[0:len(msg)-1]
        viplist.append(name)
        msg=f1.readline()
    f1.close()
    
    f1=open("susp/questionlist2.txt","r")
    msg = f1.readline().strip("\n")
    while msg :
        print msg
#        ques = msg[0:len(msg)-1]
        qlist.append(msg)
        msg=f1.readline().strip("\n")
    f1.close()
    print "start"
    
    offset = qlist.index("30008137", )
    for qid in qlist[offset:]:
        print qid
        qfile=open("susp/vext2/"+qid+".txt","w")
        qurl="http://www.zhihu.com/question/"+qid
        que=Question(qurl)
        alist = que.get_all_answers()
        for aut in alist:
            if aut in vl and aut not in viplist:
                print aut
                qfile.write(str(vl.index(aut, ))+" "+aut+"\n")
        print "finish "+qid+"\n"
Example #13
0
def question_test(url):
    question = Question(url)

    # 获取该问题的标题
    # title = question.get_title()
    # 获取该问题的详细描述
    detail = question.get_detail()
    # 获取回答个数
    answers_num = question.get_answers_num()
    # 获取关注该问题的人数
    followers_num = question.get_followers_num()
    # 获取该问题所属话题
    topics = question.get_topics()
    # 获取该问题被浏览次数
    visit_times = question.get_visit_times()
    # 获取排名第一的回答
    top_answer = question.get_top_answer()
    # 获取排名前十的十个回答
    top_answers = question.get_top_i_answers(10)
    # 获取所有回答
    answers = question.get_all_answers()

    # print title  # 输出:现实可以有多美好?
    print detail
    # 输出:
    # 本问题相对于“现实可以多残酷?传送门:现实可以有多残酷?
    # 题主:       昨天看了“现实可以有多残酷“。感觉不太好,所以我
    # 开了这个问题以相对应,希望能够“中和一下“。和那个问题题主不想
    # 把它变成“比惨大会“一样,我也不想把这个变成“鸡汤故事会“,或者
    # 是“晒幸福“比赛。所以大家从“现实,实际”的角度出发,讲述自己的
    # 美好故事,让大家看看社会的冷和暖,能更加辨证地看待世界,是此
    # 题和彼题共同的“心愿“吧。
    print answers_num  # 输出:2441
    print followers_num  # 输出:26910
    for topic in topics:
        print topic,  # 输出:情感克制 现实 社会 个人经历
    print visit_times  # 输出: 该问题当前被浏览的次数
    print top_answer  # 输出:<zhihu.Answer instance at 0x7f8b6582d0e0>(Answer类对象)
    print top_answers  # 输出:<generator object get_top_i_answers at 0x7fed676eb320>(代表前十的Answer的生成器)
    print answers  # 输出:<generator object get_all_answer at 0x7f8b66ba30a0>(代表所有Answer的生成器)
Example #14
0
def question_test(url):
    question = Question(url)

    # 获取该问题的标题
    title = question.get_title()
    # 获取该问题的详细描述
    detail = question.get_detail()
    # 获取回答个数
    answers_num = question.get_answers_num()
    # 获取关注该问题的人数
    followers_num = question.get_followers_num()
    # 获取该问题所属话题
    topics = question.get_topics()
    # 获取该问题被浏览次数
    visit_times = question.get_visit_times()
    # 获取排名第一的回答
    top_answer = question.get_top_answer()
    # 获取排名前十的十个回答
    top_answers = question.get_top_i_answers(10)
    # 获取所有回答
    answers = question.get_all_answers()

    print title  # 输出:现实可以有多美好?
    print detail
    # 输出:
    # 本问题相对于“现实可以多残酷?传送门:现实可以有多残酷?
    # 题主:       昨天看了“现实可以有多残酷“。感觉不太好,所以我
    # 开了这个问题以相对应,希望能够“中和一下“。和那个问题题主不想
    # 把它变成“比惨大会“一样,我也不想把这个变成“鸡汤故事会“,或者
    # 是“晒幸福“比赛。所以大家从“现实,实际”的角度出发,讲述自己的
    # 美好故事,让大家看看社会的冷和暖,能更加辨证地看待世界,是此
    # 题和彼题共同的“心愿“吧。
    print answers_num  # 输出:2441
    print followers_num  # 输出:26910
    for topic in topics:
        print topic,  # 输出:情感克制 现实 社会 个人经历
    print visit_times  # 输出: 该问题当前被浏览的次数
    print top_answer  # 输出:<zhihu.Answer instance at 0x7f8b6582d0e0>(Answer类对象)
    print top_answers  # 输出:<generator object get_top_i_answers at 0x7fed676eb320>(代表前十的Answer的生成器)
    print answers  # 输出:<generator object get_all_answer at 0x7f8b66ba30a0>(代表所有Answer的生成器)
Example #15
0
def test():
    url = "http://www.zhihu.com/question/24269892"
    question = Question(url)
    # 得到排名第一的答案
    answer = question.get_top_answer()
    # 得到排名第一的答案的作者
    user = answer.get_author()
    # 得到该作者回答过的所有问题的答案
    user_answers = user.get_answers()
    # 输出该作者回答过的所有问题的标题
    for answer in user_answers:
        print answer.get_question().get_title()
    # 得到该用户的所有收藏夹
    user_collections = user.get_collections()
    for collection in user_collections:
        # 输出每一个收藏夹的名字
        print collection.get_name()
        # 得到该收藏夹下的前十个回答
        top_answers = collection.get_top_i_answers(10)
        # 把答案内容转成txt,markdown
        for answer in top_answers:
            answer.to_txt()
            answer.to_md()
    def setUpClass(cls):
        url = 'http://www.zhihu.com/question/24825703'
        file_path = os.path.join(TEST_DATA_PATH, 'question.html')
        with open(file_path, 'rb') as f:
            html = f.read()
        soup = BeautifulSoup(html)

        cls.question = Question(url)
        cls.question._session = None
        cls.question.soup = soup

        act_time = datetime.datetime.fromtimestamp(1439395600)
        act_type = ActType.FOLLOW_QUESTION
        cls.activity = Activity(act_type, act_time, question=cls.question)
Example #17
0
def question_test(url):
    print "looking "+url

    question = Question(url)

    # 获取该问题的标题
    if question.isInvalid():
        # print 'invalid '+url
        return

    title = question.get_title()
    # 获取该问题的详细描述
    detail = question.get_detail()
    # 获取回答个数
    answers_num = question.get_answers_num()
    # 获取关注该问题的人数
    # followers_num = question.get_followers_num()
    # 获取该问题所属话题
    topics = question.get_topics()
    # 获取该问题被浏览次数
    visit_times = question.get_visit_times()
    # 获取排名第一的回答
    top_answer = question.get_top_answer()
    # 获取排名前十的十个回答
    top_answers = question.get_top_i_answers(10)
    # 获取所有回答
    answers = question.get_all_answers()


    for answer in top_answers:
        read=file("folder/a.txt","a+");
        if answer.get_upvote()>1000:
            print answer.get_upvote(),answer.answer_url
            read.write("\r\n"+url+"\r\n"+title+"--"+answer.get_author().get_user_id()+"\r\n"+"%d"%answer.get_upvote()+"\r\n")
        
        read.close()
Example #18
0
    def question(self, url):

        question = Question(url)

        # 获取该问题的标题
        title = question.get_title()
        # 获取该问题的详细描述
        detail = question.get_detail()
        # 获取回答个数
        answers_num = question.get_answers_num()
        # 获取关注该问题的人数
        followers_num = question.get_followers_num()
        # 获取该问题所属话题
        topics = question.get_topics()
        # 获取排名第一的回答
        # top_answer = question.get_top_answer()
        # 获取排名前十的十个回答
        top_answers = question.get_top_i_answers(10)
        # 获取所有回答
        # answers = question.get_all_answers()

        # print title # 输出:现实可以有多美好?
        # print detail
        # # 输出:
        # # 本问题相对于“现实可以多残酷?传送门:现实可以有多残酷?
        # # 题主:       昨天看了“现实可以有多残酷“。感觉不太好,所以我
        # # 开了这个问题以相对应,希望能够“中和一下“。和那个问题题主不想
        # # 把它变成“比惨大会“一样,我也不想把这个变成“鸡汤故事会“,或者
        # # 是“晒幸福“比赛。所以大家从“现实,实际”的角度出发,讲述自己的
        # # 美好故事,让大家看看社会的冷和暖,能更加辨证地看待世界,是此
        # # 题和彼题共同的“心愿“吧。
        # print answers_num # 输出:2441
        # print followers_num # 输出:26910
        # for topic in topics:
        #     print topic , # 输出:情感克制 现实 社会 个人经历
        # print top_answer # 输出:<zhihu.Answer instance at 0x7f8b6582d0e0>(Answer类对象)
        # print top_answers # 输出:<generator object get_top_i_answers at 0x7fed676eb320>(代表前十的Answer的生成器)
        # print answers # 输出:<generator object get_all_answer at 0x7f8b66ba30a0>(代表所有Answer的生成器)
        return title, detail, answers_num, followers_num, topics, top_answers
Example #19
0
def question_test(url):
    question = Question(url)

    # 获取该问题的标题
    title = question.get_title()
    # 获取该问题的详细描述
    detail = question.get_detail()
    # 获取回答个数
    answers_num = question.get_answers_num()
    # 获取关注该问题的人数
    followers_num = question.get_followers_num()
    # 获取该问题所属话题
    topics = question.get_topics()
    # 获取该问题被浏览次数
    visit_times = question.get_visit_times()
    # 获取排名第一的回答
    # top_answer = question.get_top_answer()
    # 获取排名前十的十个回答
    # top_answers = question.get_top_i_answers(10)
    # 获取所有回答
    # answers = question.get_all_answers()

    print title  # 输出:现实可以有多美好?
    print detail
    # 输出:
    # 本问题相对于“现实可以多残酷?传送门:现实可以有多残酷?
    # 题主:       昨天看了“现实可以有多残酷“。感觉不太好,所以我
    # 开了这个问题以相对应,希望能够“中和一下“。和那个问题题主不想
    # 把它变成“比惨大会“一样,我也不想把这个变成“鸡汤故事会“,或者
    # 是“晒幸福“比赛。所以大家从“现实,实际”的角度出发,讲述自己的
    # 美好故事,让大家看看社会的冷和暖,能更加辨证地看待世界,是此
    # 题和彼题共同的“心愿“吧。
    print answers_num  # 输出:2441
    print followers_num  # 输出:26910
    for topic in topics:
        print topic,  # 输出:情感克制 现实 社会 个人经历
    print visit_times  # 输出: 该问题当前被浏览的次数
Example #20
0
File: work.py Project: facert/tuiku
    def question(self, url):

        question = Question(url)

        # 获取该问题的标题
        title = question.get_title()
        # 获取该问题的详细描述
        detail = question.get_detail()
        # 获取回答个数
        answers_num = question.get_answers_num()
        # 获取关注该问题的人数
        followers_num = question.get_followers_num()
        # 获取该问题所属话题
        topics = question.get_topics()
        # 获取排名第一的回答
        # top_answer = question.get_top_answer()
        # 获取排名前十的十个回答
        top_answers = question.get_top_i_answers(10)
        # 获取所有回答
        # answers = question.get_all_answers()

        # print title # 输出:现实可以有多美好?
        # print detail 
        # # 输出:
        # # 本问题相对于“现实可以多残酷?传送门:现实可以有多残酷?
        # # 题主:       昨天看了“现实可以有多残酷“。感觉不太好,所以我
        # # 开了这个问题以相对应,希望能够“中和一下“。和那个问题题主不想
        # # 把它变成“比惨大会“一样,我也不想把这个变成“鸡汤故事会“,或者
        # # 是“晒幸福“比赛。所以大家从“现实,实际”的角度出发,讲述自己的
        # # 美好故事,让大家看看社会的冷和暖,能更加辨证地看待世界,是此
        # # 题和彼题共同的“心愿“吧。
        # print answers_num # 输出:2441
        # print followers_num # 输出:26910
        # for topic in topics:
        #     print topic , # 输出:情感克制 现实 社会 个人经历
        # print top_answer # 输出:<zhihu.Answer instance at 0x7f8b6582d0e0>(Answer类对象)
        # print top_answers # 输出:<generator object get_top_i_answers at 0x7fed676eb320>(代表前十的Answer的生成器)
        # print answers # 输出:<generator object get_all_answer at 0x7f8b66ba30a0>(代表所有Answer的生成器)
        return title, detail, answers_num, followers_num, topics, top_answers
Example #21
0
 def test_follow_question_with_id(self):
     data = Question(id=32096743).follow_question()
     self.assertEqual({"is_following": True}, data)
Example #22
0
 def test_follow_question_with_id(self):
     data = Question(id=32096743).follow_question()
     self.assertIn("is_following", data)
     self.assertIn("true", data)
Example #23
0
 def test_unfollow_question_with_url(self):
     data = Question(
         url='https://www.zhihu.com/question/58684385').unfollow_question()
     self.assertEqual('', data)
Example #24
0
 def test_follow_question_with_url(self):
     data = Question(
         url='https://www.zhihu.com/question/58684385').follow_question()
     self.assertIn("is_following", data)
     self.assertIn("true", data)
Example #25
0
 def test_unfollow_question_with_id(self):
     data = Question(id=32096743).unfollow_question()
     self.assertEqual('', data)
Example #26
0
# -*- coding: utf-8 -*-
from zhihu import Question

url = "http://www.zhihu.com/question/24269892"
question = Question(url)

# 获取该问题的标题
title = question.get_title()
# 获取该问题的详细描述
detail = question.get_detail()
# 获取回答个数
answers_num = question.get_answers_num()
# 获取关注该问题的人数
followers_num = question.get_followers_num()
# 获取该问题所属话题
topics = question.get_topics()
# 获取该问题被浏览次数
visit_times = question.get_visit_times()
# 获取排名第一的回答
top_answer = question.get_top_answer()
# 获取排名前十的十个回答
top_answers = question.get_top_i_answers(100)
# 获取所有回答
answers = question.get_all_answers()

print "Title: "
print title  # 输出:现实可以有多美好?
print "Detail: "
print detail
# 输出:
# 本问题相对于“现实可以多残酷?传送门:现实可以有多残酷?
Example #27
0
from zhihu import Zhihu

z = Zhihu()
z.user(user_slug="liuzhijun001")


def vote_up_with_id():
    data = Answer(id=14005147).vote_up()
    print(data)


logging.basicConfig(level=logging.INFO)

if __name__ == '__main__':
    # vote_up_with_id()
    from zhihu import Question
    Question(id="60231684").unfollow_question()

    # Zhihu().login("+33752962193", "zhihudetail")
    # model.login("158", "xxxx")

    # from zhihu import Zhihu
    #
    # profile  = Zhihu().user(user_slug="xiaoxiaodouzi")
    # # print(profile)
    #
    # from zhihu import Account
    #
    # account = Account()
    # account.register("续航小三", phone_num="+33752962193", password="******")
Example #28
0
from zhihu import User, UserDetail, Question
user = UserDetail("https://www.zhihu.com/people/lan-jiang-26")
t = user.get_followees()
user.get_followees_num()
# print(user.get_user_id())
print(user.get_data_id())
print(user.get_gender())
print(user.get_followees_num())
print(user.get_followers_num())
print(user.get_agree_num())
print(user.get_thanks_num())
print(user.get_asks_num())
print(user.get_answers_num())
print(user.get_collections_num())
print(user.get_profile_vote_num())
print(user.get_profile_thank_num())
print(user.get_profile_fav_num())
print(user.get_followers())
# for user in user.get_followers():
#     print(user)
t = user.get_topics()
for tt in t:
    print(tt.name)
    print(tt.id)
question = Question(url='https://www.zhihu.com/question/32046716')
print(question.get_detail())
print(question.get_topics())
Example #29
0
def download_answers(urls, topic_id, log_fname):
    answer_file_name = "Answers_text_" + topic_id + ".txt"

    # Open a file to store all answers
    if not os.path.exists(os.path.join(os.getcwd(), answer_file_name)):
        out_answer_text = open(answer_file_name, "w")
        out_answer_text.close()

    for i in range(0, len(urls)):
        url = urls[i]
        print url
        question = Question('http://www.zhihu.com' + url)

        # 获取该问题的标题
        title = question.get_title()
        # 获取该问题的详细描述
        detail = question.get_detail()
        # 获取回答个数
        answers_num = question.get_answers_num()
        # 获取关注该问题的人数
        #       try:
        #           followers_num = question.get_followers_num()
        #       except:
        #           followers_num = 0
        # 获取该问题所属话题
        topics = question.get_topics()
        # 获取该问题被浏览次数
        #       visit_times = question.get_visit_times()
        # 获取排名第一的回答
        #       top_answer = question.get_top_answer()
        # 获取排名前十的十个回答
        #       top_answers = question.get_top_i_answers(10)
        # 获取所有回答
        answers = question.get_all_answers()

        print "============"
        print "问题: \n", title
        print "问题描述: \n", detail
        print "Number of Answers: ", answers_num
        #       print followers_num
        for topic in topics:
            print topic,
#       print visit_times
#       print top_answer
#       print top_answers
        print answers

        out_answer_text = open(answer_file_name, "a")
        this_question_answers_text = []
        for answer in answers:
            author = answer.get_author()
            answer_cleaned = answer.text_one_line()
            print answer_cleaned

            this_question_answers_text.append(answer_cleaned)
#           answer.to_txt(title, detail, answers_num, followers_num, topics, visit_times, author)
#           t_sleep = random.uniform(2,4) # Stop after scraping one page.
#           print termcolor.colored(' '.join(['Sleep Time: ', str(t_sleep)]), "white")
#           time.sleep(t_sleep)
#           print "\n"
        out_answer_text.write("".join(this_question_answers_text))

        # Modify log file after successfully write the answers
        log_file = open(log_fname, "w")
        log_file.write("\n".join(urls[i:]))
        print termcolor.colored("Log modified.", "white")
        # Stop after scraping one page.
        print "\n--------------\n--------------"
        t_sleep = random.uniform(1, 2)
        print termcolor.colored(' '.join(['Sleep Time: ',
                                          str(t_sleep)]), "red")
        time.sleep(t_sleep)
    out_answer_text.close()
Example #30
0
 def __init__(self, question_id):
     self.question_id = question_id
     que = Question(prefix_question + question_id)
     self.topics = que.get_topics()
Example #31
0
 def test_follow_question_with_url(self):
     data = Question(
         url='https://www.zhihu.com/question/58684385').follow_question()
     self.assertEqual({"is_following": True}, data)
Example #32
0
 def __init__(self, question_id):
     self.question_id = question_id
     que = Question(prefix_question + question_id)
     self.topics = que.get_topics()
Example #33
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from zhihu import Question

url = "http://www.zhihu.com/question/25378124"
question = Question(url)
answers = question.get_all_answers()
for answer in answers:
    answer.to_txt()
    answer.to_md()