dataabc / weiboSpider

新浪微博爬虫,用python爬取新浪微博数据
8.37k stars 1.98k forks source link

仅爬取原创微博但当获取某页都是转发微博时,遍历中止 #232

Closed bluerthanever closed 3 years ago

bluerthanever commented 4 years ago

感谢您申报bug,为了表示感谢,如果bug确实存在,您将出现在本项目的贡献者列表里;如果您不但发现了bug,还提供了很好的解决方案,我们会邀请您以pull request的方式成为本项目的代码贡献者(Contributor);如果您多次提供很好的pull request,我们将邀请您成为本项目的协助者(Collaborator)。当然,是否提供解决方按都是自愿的。不管是否是真正的bug、是否提供解决方案,我们都感谢您对本项目的帮助。

https://github.com/dataabc/weiboSpider/blob/16fd1d9dabb9839d3562ad61ed93c7eb577a41e7/weibo_spider/spider.py#L158-L161 这个部分,比如我的filter设置的1,也就是只获取原创微博的时候,如果某一页全部都是转发的微博,直接就return了,后面的页数都获取不到了

bluerthanever commented 4 years ago

还有一个前提,就是,我设置的since_date是1900-01-01,所以不应该这样。 但是我也注意到,好像跳出循环也是通过return来判断的。。。

bluerthanever commented 4 years ago
    def get_one_page(self, weibo_id_list):
        """获取第page页的全部微博"""
        try:
            info = self.selector.xpath("//div[@class='c']")
            is_exist = info[0].xpath("div/span[@class='ctt']")
            weibos = []
            if is_exist:
                since_date = datetime_util.str_to_time(self.since_date)
                for i in range(0, len(info) - 2):
                    weibo = self.get_one_weibo(info[i])
                    if weibo:
                        if weibo.id in weibo_id_list:
                            continue
                        publish_time = datetime_util.str_to_time(
                            weibo.publish_time)

                        if publish_time < since_date:
                            if self.is_pinned_weibo(info[i]):
                                continue
                            else:
                                return weibos, weibo_id_list, False
                        logger.info(weibo)
                        logger.info('-' * 100)
                        weibos.append(weibo)
                        weibo_id_list.append(weibo.id)
            return weibos, weibo_id_list, True
        except Exception as e:
            logger.exception(e)

我直接改成这样了,然后上面调用这个函数的时候,拆出三个变量,在上面158行的地方if判断这第三个变量大概就可以了吧。。。没仔细测试

Edit: 顺带一提上面的PageParser的get_one_page函数我只是改了return值,加了第三个返回的值,其他的没动。 不知道你们会觉得是放在外面判断好还是怎样。 另外就是207的bug,估计pageparser还要改吧。。。

dataabc commented 4 years ago

感谢反馈。

这里确实有问题,建议很好。因为是你发现并提出了解决方案,能否通过pull request提交修改,这样你也可以成为本项目的Contributor。这不是强制的,只是因为这是你的劳动成果,所以提出这个请求,如果明天之前没有提交pull request,我会自己修改。

再次感谢,如果还有建议欢迎继续讨论:smile:

bluerthanever commented 4 years ago

233

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.