Spring Boot 爬蟲從入門到精通
- 匯入HttpClient需要的jar包
- 匯入Jsoup需要的jar包
- 一、HttpClient 入門
- 二、正則運算式實體
- 三、Jsoup:Java的HTML決議器
- Gitee原始碼下載
匯入HttpClient需要的jar包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
匯入Jsoup需要的jar包
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
一、HttpClient 入門
常用的39個用戶代理:點我查看
public void doGetTestOne() throws IOException{
// 獲得Http客戶端
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 創建Get請求
HttpGet httpGet = new HttpGet("http://www.grfy.net/index.htm");
// 設定請求頭:User-Agent用戶代理(不設定個別網站訪問不了)
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
// 回應模型
CloseableHttpResponse response = null;
try {
// 由客戶端執行(發送)Get請求
response = httpClient.execute(httpGet);
// 從回應模型中獲取回應物體
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
//獲取網頁源代碼
String first_page = EntityUtils.toString(responseEntity, "utf-8");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 釋放資源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
二、正則運算式實體
//創建一個存放目的資料的集合
List<String> list = new ArrayList<String>();
//撰寫正則運算式
String regex = "<a href=\"(http://.*?net)\">";
//Pattern與Matcher一起合作.
//Matcher類提供了對正則運算式的分組支持,以及對正則運算式的多次匹配支持
Pattern pattern = Pattern.compile("正則運算式");
Matcher matcher = pattern.matcher("網頁原始碼");
//將Matcher物件中的資料存盤到list中
while (matcher.find()) {
list.add(matcher.group(1));
}
//遍歷輸出list陣列
for (String str : list) {
if (str.length() < 30) {
System.out.println("二級網頁:" + str);
}
}
三、Jsoup:Java的HTML決議器
//將網頁源代碼轉化為Document物件
Document doc = Jsoup.parse(mubiao_page);
//查詢當前標簽第一個選中的第一個文本內容
String title = doc.getElementsByTag("h1").first().text();
//查詢當前選中元素上一個元素
//Elements elements = doc.getElementsByTag("dd").prevAll();
//查詢當前選中元素下一個元素
Elements elements = doc.getElementsByTag("dt").nextAll();
//獲取迭代器
Iterator it = elements.iterator();
int i = 1;
//創建Home物件
Home home = new Home();
home.setTitle(title);
//對獲取的資料進行迭代遍歷,存盤到Home中
while (it.hasNext()) {
Element element = (Element) it.next();
String text = element.text();
if (i == 1) {
home.setPrice(text);
} else if (i == 2) {
home.setArea(text);
} else if (i == 3) {
home.setName(text);
} else if (i == 4) {
home.setAdress(text);
} else if (i == 5) {
home.setMsg(text);
} else if (i == 6) {
home.setHight(text);
} else if (i == 7) {
home.setPeople(text);
} else if (i == 8) {
home.setPhone(text);
}
i = i + 1;
}
Gitee原始碼下載
點我下載:https://gitee.com/pipizi/Pc_home/tree/master/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/203086.html
標籤:其他
