第一次寫爬蟲嗦枢,案例比較簡(jiǎn)單,就在智聯(lián)招聘網(wǎng)站上爬取職位信息屯断。
技術(shù)點(diǎn):IO流文虏,集合,Jsoup使用殖演,以及前端的知識(shí)
代碼如下:
package com.wty.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
*
*<p>Title:智聯(lián)招聘網(wǎng)站爬蟲小案例</p>
*<p>Description:</p>
*<p>Company:</p>
*@author wty
*@date 2017年3月11日上午11:28:01
*
*/
public class DownWebPageUtils {
public static String getHtmlCode(String url, String encoding) {
URL uri =null;
URLConnection urlConnection =null;
InputStream inputStream =null;
InputStreamReader inputStreamReader = null;
BufferedReader bReader =null;
StringBuffer sBuffer= new StringBuffer();
try {
// 建立網(wǎng)絡(luò)連接
uri = new URL(url);
// 打開(kāi)連接
urlConnection = uri.openConnection();
//輸入流
inputStream = urlConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream, encoding);
bReader = new BufferedReader(inputStreamReader);
String temp;
while ((temp = bReader.readLine()) != null) {
sBuffer.append(temp + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//關(guān)閉資源
if(bReader!=null){
try {
bReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sBuffer.toString();
}
public static List<HashMap<String, String>> analyzeHtml(String url, String encoding){
String htmlCode = getHtmlCode(url, encoding);
Document document = Jsoup.parse(htmlCode);
Elements elements = document.getElementsByClass("newlist");
List<HashMap<String, String>> list=new ArrayList<>();
for (Element e : elements) {
HashMap<String, String> map = new HashMap<>();
String zwmc = e.getElementsByClass("zwmc").text();
String gsmc = e.getElementsByClass("gsmc").text();
String zwyx = e.getElementsByClass("zwyx").text();
String gzdd = e.getElementsByClass("gzdd").text();
String gxsj = e.getElementsByClass("gxsj").text();
map.put("職位名稱:", zwmc);
map.put("公司名稱", gsmc);
map.put("職位月薪", zwyx);
map.put("工作地點(diǎn)", gzdd);
map.put("發(fā)布日期", gxsj);
list.add(map);
}
return list;
}
public static void main(String[] args) {
List<HashMap<String, String>> resultList = analyzeHtml("http://sou.zhaopin.com/jobs/searchresult.ashx?jl=%E4%B8%8A%E6%B5%B7&kw=java&sm=0&p=1", "UTF-8");
System.out.println(resultList);
}
}
數(shù)據(jù)可以在控制臺(tái)中打印出來(lái)氧秘,至于jsp頁(yè)面的展現(xiàn)筆者還沒(méi)寫,無(wú)非就是表格趴久,循環(huán)什么的丸相。有興趣的話可以寫一下。
感覺(jué)爬蟲還蠻好玩的彼棍。下次準(zhǔn)備爬個(gè)美圖網(wǎng)站灭忠。。座硕。hhhhh