java擷取url內容及正則匹配連結圖片地址

來源:互聯網
上載者:User

import java.net.*;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;

public class QuestionResult {
 private Socket socket;
 private String host = "www.01hr.com";
 private int port = 80;
 private String filePath = "E://index.txt";
 private ArrayList urlList, imgList;

 // 建立socket連結
 public void initSocket() throws Exception {
  socket = new Socket(host, port);
 }
 // 取得頁面內容方法1
 public void getHttpUrlContent() throws Exception {
  URL url = new URL("http://www.01hr.com");
  InputStream input = url.openStream();
  BufferedReader br = new BufferedReader(new InputStreamReader(input));
  String data;
  File file = new File(filePath);
  FileWriter fw = new FileWriter(file);
  BufferedWriter bw = new BufferedWriter(fw);
  while ((data = br.readLine()) != null) {
   matchUrl(data);
   matchImage(data);
   bw.write(data);
   bw.newLine();
  }
  bw.close();
  fw.close();
  br.close();
  input.close();
  
 }

 // 取得頁面內容方法2
 public void getHttpSocketContent() throws Exception {
  StringBuffer sb = new StringBuffer("GET " + "/index.html"
    + " HTTP/1.1/r/n");
  sb.append("Host: " + host + "/r/n");
  sb.append("Accept: */*/r/n");
  sb.append("Accept-Language: zh-cn/r/n");
  sb.append("Accept-Encoding: gzip, deflate/r/n");
  sb.append("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)/r/n");
  sb.append("Connection: Keep-Alive/r/n/r/n");

  OutputStream socketOut = socket.getOutputStream();
  socketOut.write(sb.toString().getBytes());
  
  InputStream socketIn = socket.getInputStream();
  BufferedReader br = new BufferedReader(new InputStreamReader(socketIn));
  
  File file = new File(filePath);
  FileWriter fw = new FileWriter(file);
  BufferedWriter bw = new BufferedWriter(fw);
  
  String data;
  int i = 0;
  while ((data = br.readLine()) != null) {
   if (i > 9) {// 不顯示返回報文
    matchUrl(data);
    matchImage(data);
    bw.write(data);
    bw.newLine();
   }
   i++;
  }

  bw.close();
  fw.close();
  br.close();
  socketIn.close();
  socketOut.close();
  socket.close();
 }
 //匹配連結內容
 public void matchUrl(String str) {
  urlList = new ArrayList();
  String regEx = "<a.*?href=/"(.*?)/"";
  Pattern pat = Pattern.compile(regEx);
  Matcher mat = pat.matcher(str);
  while (mat.find()) {
   urlList.add(mat.group(1));
  }
 }
 //匹配圖片內容
 public void matchImage(String str) {
  imgList = new ArrayList();
  String regEx = "<img.*?src=/"(.*?)/"";
  Pattern pat = Pattern.compile(regEx);
  Matcher mat = pat.matcher(str);
  while (mat.find()) {
   imgList.add(mat.group(1));
  }
 }
 //列印匹配後的地址
 public void printContent(String contentName,ArrayList al){
  System.out.println(contentName);
  for(int i=0;i<al.size();i++){
   System.out.println(al.get(i));
  }
 }

 public static void main(String args[]) throws Exception {
  QuestionResult client = new QuestionResult();  
 /* client.initSocket();//取得頁面內容方法2
  client.getHttpSocketContent();*/
  client.getHttpUrlContent();////取得頁面內容方法1
  client.printContent("---------------連結地址列印---------------",client.urlList);
  client.printContent("---------------圖片地址列印---------------",client.imgList);
 }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.