示例#1
0
def crawl_lagou_job_data_task(lagou_company_id):
    """爬取拉勾职位数据任务"""
    # 过滤本轮已经爬取过职位的公司
    if not redis_instance.setnx(constants.CRAWLED_COMPANY_JOBS_REDIS_KEY.format(lagou_company_id=lagou_company_id), 1):
        return
    jobs_pagination = crawlers.get_jobs_pagination_from_lagou(lagou_company_id=lagou_company_id,
                                                              job_type=constants.LagouJobType.technology)
    for page_no in jobs_pagination.iter_pages:
        job_dicts = crawlers.get_jobs_from_lagou(lagou_company_id=lagou_company_id,
                                                 job_type=constants.LagouJobType.technology,
                                                 page_no=page_no)
        if not job_dicts:
            break
        for job_dict in job_dicts:
            crawlers.clean_lagou_job_data(job_dict)
            utils.convert.convert_dict_field_to_constants(job_dict)

            keywords = job_dict.pop('keywords')
            city_name = job_dict.pop('city_name')

            city_ctl.insert_city_if_not_exist(city_name)
            job_dict['city_id'] = city_ctl.get_city_id_by_name(city_name)
            company = CompanyModel.get_one(filter_by={'lagou_company_id': lagou_company_id})
            job_dict['company_id'] = company.id

            job = JobModel.get_one(filter_by={'lagou_job_id': job_dict.lagou_job_id})
            if job:
                JobModel.update_by_pk(pk=job.id, values=job_dict)
            else:
                job_id = JobModel.add(**job_dict)

                for keyword in keywords:
                    keyword_ctl.insert_keyword_if_not_exist(name=keyword)
                    keyword_id = keyword_ctl.get_keyword_id_by_name(name=keyword)
                    JobKeywordModel.add(keyword_id=keyword_id, job_id=job_id)
示例#2
0
 def test_update_by_pk(self):
     affect_rows = JobModel.update_by_pk(pk=6814, values={'title': '你好啊啊'})
     self.assertEqual(affect_rows, 1)
     self.assertEqual(JobModel.get_by_pk(pk=6814).title, u'你好啊啊')