根据条件保存抓取到的信息
来自CloudWiki
这里根据条件筛选岗位信息,比如已经列出了一些培训机构的“公司黑名单”,保存在了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);
}