def setUpClass(cls): url = 'http://zhuanlan.zhihu.com/xiepanda' file_path = os.path.join(TEST_DATA_PATH, 'column.json') with open(file_path, 'r') as f: soup = json.load(f) post_path = os.path.join(TEST_DATA_PATH, 'column_post.json') with open(post_path, 'r') as f: cls.post_json = json.load(f) cls.column = Column(url) cls.column.soup = soup cls.expected = { 'name': '谢熊猫出没注意', 'follower_num': 76605, 'post_num': 69, 'post_author_id': 'xiepanda', 'post_title': ("为了做一个称职的吃货,他决定连着吃" "一百天转基因食物"), 'post_upvote_num': 963, 'post_comment_num': 199 }
def column_test(column_url): column = Column(column_url) # 获取该专栏的标题 title = column.get_title() # 获取该专栏的描述 description = column.get_description() # 获取该专栏的作者 creator = column.get_creator() # 获取该专栏的文章数 posts_num = column.get_posts_num() # 获取该专栏的所有文章 posts = column.get_all_posts() print title print description print creator # 输出:<zhihu.User instance at 0x7f8b6582d0e0> # User类对象 print posts_num print posts
def test_followers_with_slug(self): slug = "pythoneer" data = Column(slug=slug).followers(limit=10) self.assertEqual(10, len(data))
def test_followers_with_url(self): url = "https://zhuanlan.zhihu.com/pythoneer" data = Column(url=url).followers(limit=10) self.assertEqual(10, len(data))