Example #1
0
    def __init__(self, url=None, noticer_list=None):
        if not noticer_list:  # 创建一个新的Noticer
            try:
                if Noticer.client:
                    self.name = Noticer.client.Author(url).name
                else:
                    self.name = zhihu.Author(url).name
            except AttributeError:
                raise UrlError
            except ValueError:
                raise UrlError

            self.notice_methods = [notice_method for notice_method in zhihu.ActType]
            notice_methods_in_json = [notice_method.value for notice_method in zhihu.ActType]
            self.url = url
            self.latest_act_url = None

        else:  # 从json里导入时
            self.url = noticer_list[0]
            notice_methods_in_json = noticer_list[1]
            self.latest_act_url = noticer_list[2]
            self.name = noticer_list[3]
            self.notice_methods = list()

            for act_type in zhihu.ActType:
                for notice_method in noticer_list[1]:
                    if act_type.value == notice_method:
                        self.notice_methods.append(act_type)

        self.list = [self.url, notice_methods_in_json, self.latest_act_url, self.name]
Example #2
0
def test_author():
    url = 'http://www.zhihu.com/people/7sdream'
    author = zhihu.Author(url)
    # 获取用户名称
    print(author.name)
    # 7sDream

    # 获取用户介绍
    print(author.motto)
    # 二次元新居民/软件爱好者/零回答消灭者

    # 获取用户关注人数
    print(author.followees_num)
    # 66

    # 获取用户粉丝数
    print(author.followers_num)
    # 179

    # 获取用户得到赞同数
    print(author.agree_num)
    # 1078

    # 获取用户得到感谢数
    print(author.thanks_num)
    # 370

    # 获取用户提问数
    print(author.questions_num)
    # 16

    # 获取用户答题数
    print(author.answers_num)
    # 227

    # 获取用户专栏文章数
    print(author.post_num)
    # 0

    # 获取用户收藏夹数
    print(author.collections_num)
    # 5

    # 获取用户所有提问
    print(author.questions)
    # <generator object questions at 0x0156BF30>

    # 获取用户所有回答
    print(author.answers)
    # <generator object answers at 0x0156BF30>

    # 获取用户所有收藏夹
    print(author.collections)
    # <generator object collections at 0x0156BF30>

    # 对 generator 可执行迭代操作, 这里用Collection举例
    for collection in author.collections:
        print(collection.name)
Example #3
0
def save2md(url, min_upvote=-1, max_number=1000):
    answers = zhihu.Author(url).answers

    file_name = get_file_name(url, min_upvote)

    file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                        file_name + '.md')

    write_md(answers, file, min_upvote, max_number)
Example #4
0
def get_file_name(url, min_upvote):
    if min_upvote == -1:
        file_name = zhihu.Author(url).name + '最近回答'
    else:
        file_name = zhihu.Author(url).name + min_upvote + '+赞回答锦集'
    return file_name