jsoup 是一款 java 的 html 解析器,可直接解析某个 url 地址、html 文本内容。它提供了一套非常省力的 api,可通过 dom,css 以及类似于 jquery 的操作方法来取出和操作数据。
下面是招聘网站的html信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
<div class = "newlist_list_content" id= "newlist_list_content_table" > <table width= "853" class = "newlist" cellpadding= "0" cellspacing= "0" > <tbody> <tr> <th class = "zwmc" ><span>职位名称</span></th> <th class = "gsmc" >公司名称</th> <th class = "zwyx" >职位月薪</th> <th class = "gzdd" >工作地点</th> <th class = "gxsj" >发布日期</th> </tr> </tbody> </table> <table cellpadding= "0" cellspacing= "0" width= "853" class = "newlist" > <tbody> <tr> <td class = "zwmc" > <input type= "checkbox" name= "vacancyid" value= "cc415107716j90250224000_635_1_03_201__1_" onclick= "zlapply.uncheckall('allvacancyid')" /> <div style= "width:300px;float:left" > <a style= "font-weight: bold" par= "ssidkey=y&ss=201&ff=03" href= "http://jobs.zhaopin.com/415107716250224.htm" rel= "external nofollow" target= "_blank" >android 开发工程师</a> </div> </td> <td class = "gsmc" ><a href= "http://special.zhaopin.com/pagepublish/41510771/index.html" rel= "external nofollow" target= "_blank" >南京天洑软件有限公司</a></td> <td class = "zwyx" >面议</td> <td class = "gzdd" >南京</td> <td class = "gxsj" ><span> 10 - 24 </span><a class = "newlist_list_xlbtn" href= "javascript:;" rel= "external nofollow" ></a></td> </tr> <tr style= "display: none" class = "newlist_tr_detail" > <td width= "833px" style= "line-height: 0;" colspan= "5" > <div class = "newlist_detail" > <div class = "clearfix" > <ul> <li class = "newlist_deatil_two" ><span>地点:南京</span><span>公司性质:民营</span><span>公司规模: 20 - 99 人</span><span>经验: 1 - 3 年</span><span>学历:大专</span></li> <li class = "newlist_deatil_last" > 岗位职责: 1 、根据需求,基于android平台进行程序开发; 2 、根据产品功能模块设计,编码实现各模块功能,并确保开发质量; 3 、编写相关的开发文档。 任职要求: 1 、大专以上学历, 计算机或相关专业者优先; 2 、 2 年以上<b>android开发</b>经验; 3 、熟悉java编...</li> </ul> <dl> <dt> <a href= "javascript:void(0)" rel= "external nofollow" onclick= "return zlapply.searchjob.ajaxapplybrig1('cc415107716j90250224000_635','ssi','_1_03_201__2_')" > <img src= "/assets/images/newlist_sqimg_03.jpg" /> </a> </dt> <dd> <a href= "javascript:zlapply.searchjob.saveone('cc415107716j90250224000_635')" rel= "external nofollow" ><img src= "/assets/images/newlist_scimg_06.jpg" /></a> </dd> </dl> </div> </div> </td> </tr> </tbody> </table> |
下面使用 jsoup解析html获取招聘信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
public static list<htmlfeed> parse(string html) { document doc = jsoup.parse(html); elements elements = doc.getelementsbyclass( "newlist" ).select( "tr" ); list<htmlfeed> list= new arraylist<htmlfeed>(); for (element ele : elements) { if (!ele.select( "td" ).tostring().equals( "" )) { string job_url = ele.getelementsbyclass( "zwmc" ).select( "a" ).attr( "href" ); string job = ele.getelementsbyclass( "zwmc" ).text(); string company = ele.getelementsbyclass( "gsmc" ).text(); string addr = ele.getelementsbyclass( "gzdd" ).text(); string date = ele.getelementsbyclass( "gxsj" ).text(); htmlfeed feed = new htmlfeed(); if (!job_url.tostring().equals( "" )&&!job.tostring().equals ( "" )&&!addr.tostring().equals( "" )&&!company.tostring().equals( "" )&&!date.tostring().equals( "" )) { feed.setjob_url(job_url.tostring()); feed.setjob(job.tostring()); feed.setaddr(addr.tostring()); feed.setcompany(company.tostring()); feed.setdate(date.tostring()); list.add(feed); } } } return list; } |
效果图如下:
效果图如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。