今日头条爬虫分析-爬取用户发的所有内容

今日头条的用户页数据爬取跟频道页的数据爬取大部分很类似,但稍微有一点不一样,就是用户主页的接口signature有点不一样,需要将当前爬取的用户id和分页时间戳一起作为入参传递进去才能获取到真正的signature,除了这一点差异外其他的都是一样的思路,上代码:

    def fetch_user_articles(self, user, browser):
        honey = json.loads(self.get_js())
        signature = honey['_signature']
        max_behot_time = "0"
        _as = honey['as']
        cp = honey['cp']
        if self.user_page > 0:
            signature = browser.execute_script("return window.TAC.sign(" + user.user_id+max_behot_time + ")")
        headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
            'Connection': 'keep-alive',
            'authority': 'www.toutiao.com',
            'referer': user.media_url,
            'method': 'GET',
            'path': "/c/user/article/?page_type=1&user_id={}&max_behot_time={}&count=20&as={}&cp={}&_signature={}".format(
                user.user_id, max_behot_time, _as, cp, signature),
            'scheme': 'https'
        }
        self.s.headers.update(headers)
        req_url = "https://www.toutiao.com/c/user/article/?page_type=1&user_id={}&max_behot_time={}&count=20&as={}&cp={}&_signature={}".format(
            user.user_id, max_behot_time, _as, cp, signature)
        req = self.s.get(req_url, proxies=get_proxy_ip())
        # 通过随机数控制请求速度
        time.sleep(random.random() * 2 + 2)
        data = json.loads(req.text)
        max_behot_time = str(data['next'][max_behot_time])
        if data['has_more']:
            self.user_page = self.user_page + 1
            self.parse_user_artcle(data['data'], toutiaoitem.user_id, toutiaoitem.media_url)
            #在休眠2s
            time.sleep(2)
            self.fetch_user_articles(user, browser)
        else:
            self.parse_user_artcle(data['data'], toutiaoitem.user_id, toutiaoitem.media_url)
            toutiaodb.save(self.user_artcile_list)

    def parse_user_artcle(self, items, user_id, url):
        for item in items:
            toutiaoitem = toutiaoitem()
            toutiaoitem.user_id = user_id
            toutiaoitem.source = item['source']
            toutiaoitem.title = item['title']
            toutiaoitem.source_url = 'https:' + item['display_url']
            toutiaoitem.media_url = url
            toutiaoitem.item_id = item['item_id']
            toutiaoitem.abstract = item['abstract']
            toutiaoitem.comments_count = item['comments_count']
            toutiaoitem.behot_time = item['behot_time']
            toutiaoitem.image_url = item['image_url']
            toutiaoitem.image_list = item['image_list']
            toutiaoitem.tag = item['tag']
            toutiaoitem.chinese_tag = item['chinese_tag']
            toutiaoitem.read_count = item['go_detail_count']
            toutiaoitem.article_genre = item['article_genre']
            self.user_artcile_list.append(toutiaoitem)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,641评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,993评论 2 59
  • 记得有句话说,灵魂和身体,必须要有一个在路上。我想,这是没错的,但同样重要的是,灵魂与身体也必须有一个在家里...
    一只社畜的诗意生活阅读 301评论 1 1
  • 小美女 20180527
    疏影0701阅读 194评论 1 1
  • 布雷迪的猴子 布雷迪不是一座山,也不是茂密的森林,而是一位科学家的名字。 那是一个晴朗的日子,两只猴子坐在他们的位...
    NANA0阅读 331评论 0 0