Skip to content
This repository has been archived by the owner on Jul 21, 2022. It is now read-only.

请问有获取话题日志的接口吗? #86

Open
strrchr opened this issue Sep 24, 2016 · 3 comments
Open

请问有获取话题日志的接口吗? #86

strrchr opened this issue Sep 24, 2016 · 3 comments

Comments

@strrchr
Copy link

strrchr commented Sep 24, 2016

从zhihu-oauth项目找到这个项目,发现topic.py文件中也没有获取话题日志的接口。
不知道能否实现一下?

@7sDream
Copy link
Owner

7sDream commented Sep 24, 2016

可以实现,但是我现在在忙其他项目基本没什么时间管这个项目了……而且这需求也太小众了吧……

要不你自己 Fork 一个实现一下来个 PR 呗……

@strrchr
Copy link
Author

strrchr commented Sep 24, 2016

不好意思,不会玩PR。这个获取话题日志的代码留作参考:

# common.py
Topic_Get_More_Log_Url = Topic_Url + '/{0}/log'

# topic.py
    @property
    def log(self):
        """获取话题日志

        :return: 话题日志,返回生成器
        :rtype: div.Iterable
        """
        self._make_soup()
        gotten_data_num = 20
        data = None
        offset = 0
        while gotten_data_num == 20:
            if data is None:
                res = self._session.get(Topic_Get_More_Log_Url.format(self.id))
                data = {
                    '_xsrf': self.xsrf, # 看浏览器没加这个参数,但这里不加就403
                }
                #print(res)
                gotten_data_num = 20
                soup = BeautifulSoup(res.content)
            else:
                #print(data)
                res = self._session.post(Topic_Get_More_Log_Url.format(self.id), data=data)
                #print(res)
                j = res.json()
                assert(j['r'] == 0)
                j = j['msg']
                gotten_data_num = j[0]
                soup = BeautifulSoup(j[1])
            assert(gotten_data_num <= 20)
            offset += gotten_data_num
            data['offset'] = str(offset)
            #data['offset'] = "0"
            #del data['offset']
            #data['offset'] = "0xxxxx"
            divs = soup.find_all('div', class_='zm-item')
            assert(len(divs) == gotten_data_num)
            for div in divs:
                yield div
            data['start'] = re_get_number.match(divs[-1]['id']).group(1)

yield出来的是BeautifulSoup对象,需要在应用层面做解析,解析逻辑似乎挺麻烦的。

@7sDream
Copy link
Owner

7sDream commented Sep 24, 2016

嗯嗯,可以~等我闲下来就去加上。

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants