人生不过短短几十年,莫虚度光阴,到老抱怨年轻的自己 ——行人

java爬虫利用jsoup爬取前程无忧招聘信息数据

好久不发帖了,主要原因是因为我懒,最近想换工作了,所以写了个前程无忧的爬虫,能够爬取前程无忧的招聘信息
就这些,爬出来之后输出为csv格式的数据

 

 ++++++++++++++++++++++++++++++教学++++++++++++++++++++++++++++++++++++++
基础的就不说了,前程无忧的数据是放在script标签里所以,存储格式是json(如下图),看到没,前程无忧多好,数据都帮你处理好了,拿走就是

 这部分代码

URL url = new URL("https://search.51job.com/list/020000,000000,0000,00,9,99,"+name+",2,1.html?lang=c&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare=");
            Document document = Jsoup.parse(url, 4000);
            Elements script = document.getElementsByTag("script");
            String jsonString=null;
            for (Element element : script) {
                String text = element.html();
                if(!StringUtil.isBlank(text)&&text.contains("__SEARCH_RESULT__")){
                    jsonString=text.replace("window.__SEARCH_RESULT__ = ","");
                    break;
                }
            }

jsonString就是最终获取到的json格式字符串了,接下来把它转换成JSON数据就是了,拆分下json格式

 

JSON数据解读
其中market_ads是前程无忧官方的招聘,就是展示在最上面的公司
engine_jds是我们要的数据(所有公司的招聘信息)
total_page 是总页数
我们本次就用到这些字段,然后我们解读下engine_jds中的数据,engine_jds是个集合包含所有数据,这是其中一条数据(下图)
这些是我们需要的字段
公司名 company_name
公司主页 company_href
公司类型 companytype_text
公司规模 companysize_text
招聘主页 job_href
招聘标题名 job_title
薪资范围 providesalary_text
工作地点 workarea_text
发布日期 issuedate
工作类型 companyind_text
公司福利 jobwelf
工作区域|经验要求|学历要求|招聘人数 attribute_text

这部分代码

 
if(jsonString!=null){
                    JSONObject jsonObject1=JSON.parseObject(jsonString);
                    JSONArray engine_jds = jsonObject1.getJSONArray("engine_jds");
                    for (int k = 0; k <engine_jds.size(); k++) {
                        List<String> data=new ArrayList<>();
                        JSONObject jsonObject2 = engine_jds.getJSONObject(k);
                        //公司名 company_name
                        String company_name = jsonObject2.getString("company_name");
                        data.add(company_name);
 
                        //公司主页 company_href
                        String company_href = jsonObject2.getString("company_href");
                        data.add(company_href);
 
                        //公司类型 companytype_text
                        String companytype_text = jsonObject2.getString("companytype_text");
                        data.add(companytype_text);
 
                        //公司规模 companysize_text
                        String companysize_text = jsonObject2.getString("companysize_text");
                        data.add(companysize_text);
 
                        //招聘主页 job_href
                        String job_href = jsonObject2.getString("job_href");
                        data.add(job_href);
 
                        //招聘标题名 job_title
                        String job_title = jsonObject2.getString("job_title");
                        data.add(job_title);
 
                        //薪资范围 providesalary_text
                        String providesalary_text = jsonObject2.getString("providesalary_text");
                        data.add(providesalary_text);
 
                        //工作地点 workarea_text
                        String workarea_text = jsonObject2.getString("workarea_text");
                        data.add(workarea_text);
 
                        //发布日期 issuedate
                        String issuedate = jsonObject2.getString("issuedate");
                        data.add(issuedate);
 
                        //工作类型 companyind_text
                        String companyind_text = jsonObject2.getString("companyind_text");
                        data.add(companyind_text);
 
                        //公司福利 jobwelf
                        String jobwelf = jsonObject2.getString("jobwelf");
                        data.add(jobwelf);
                        //工作区域|经验要求|学历要求|招聘人数 attribute_text
                        JSONArray attribute_text = jsonObject2.getJSONArray("attribute_text");
                        String collect = attribute_text.stream().map(String::valueOf).collect(Collectors.joining("|"));
                        data.add(collect);
                        //工作区域
                        // String address=attribute_text.getString(0);
                        //经验要求
                        // String experience=attribute_text.getString(1);
                        //学历要求
                        // String education=attribute_text.getString(2);
                        //招聘人数
                        // String recruitsNumber=attribute_text.getString(3);
                        list.add(data);
                    }
                }
 
            }

然后我们获取到了所有数据,至于想咋处理这些数据,我是输出csv,这部分就不说了,自己看代码吧

项目代码: https://wwi.lanzoui.com/ipvClvvhxqd 密码:7nig

 

posted @ 2021-10-28 17:35  夜雨步行人  阅读(12)  评论(0)    收藏  举报