查看“根据条件保存抓取到的信息”的源代码
←
根据条件保存抓取到的信息
跳转至:
导航
,
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
这里根据条件筛选岗位信息,比如已经列出了一些培训机构的“公司黑名单”,保存在了Dictionary类中的Blacklist静态String数组中,那么可以用简单的if语句来判断: Boolean choose = true; //判断是否为培训公司发布的“垃圾”招聘信息 for (String i:Dictionary.Blacklist){ if ( ((String) resultItems.get("company")).contains(i)){ choose=false; } } 对于发布日期,仅需要保留当天发布的招聘信息,判断代码如下: //判断是否为当天发布的招聘信息 if (!((String) resultItems.get("createtime")).contains(Today_Date)){ choose=false; } 其中Today_Date是一个静态final型字符串: public static final String Today_Date = timeTostrMD(new Date()); timeToStrMD是一个自定义获取当天月份和日期并返回诸如“05-05”形式字符串的方法。 代码如下: public static String timeTostrMD(Date date) { String strDate = ""; if (date != null) { SimpleDateFormat format = new SimpleDateFormat("MM-dd"); strDate = format.format(date); } return strDate; } 在筛选完之后,便可以将抓取内容选择性地保存至本地的某个文件中,这里定制的保存格式为一个岗位保存一行,每行中的信息用制表符\t分隔,最后保存在一个以日期命名的文本文件中,代码如下: try { //定义存储路径,以每天为一个文件存储 String path = this.path + PATH_SEPERATOR + timeTostrYMD(new Date()) + ".txt"; File file = getFile(path); String str =resultItems.get("url") +"\t" +resultItems.get("job") +"\t" + resultItems.get("location") + "\t" + resultItems.get("company") + "\t" + resultItems.get("salary") + "\t" + resultItems.get("experience") + "\t" + resultItems.get("education") + "\t" + resultItems.get("number") + "\t" + resultItems.get("createtime") + "\t" + replaceBlank(delHTMLTag(resultItems.get("content") .toString())) + "\r\n"; FileUtils.writeStringToFile(file, str, "utf-8", true); } catch (IOException e) { logger.warn("write file error", e); }
返回至
根据条件保存抓取到的信息
。
导航菜单
个人工具
登录
命名空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息