- 取得連結
- X
- 以電子郵件傳送
- 其他應用程式
科技出自於人性,當你想在網路這片深不見底的海洋查詢特定的資料時,就有如大海撈針,於是網路爬蟲便應運而生
總而言之,以下是我在學校學到的JAVA爬蟲程式碼,只有最基本的功能,用起來有時候還會有編碼的問題
不過這只是第一版,之後有空的話我會再研究看看,然後更新一下(嗯~有空的話
2020/05/06
步驟:
1.輸入網址
2.輸入目標關鍵字
3.輸入要回傳關鍵字後面的幾個字
EX:
https://enwww.ncl.edu.tw/
Tel:
14
RETURN:886-2-23619132
以下是程式碼:
import java.io.*;import java.net.HttpURLConnection;import java.net.URL;import java.util.Scanner;public class Main {public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
String url = sc.nextLine();
String goal = sc.nextLine();
int len = sc.nextInt();
String page = get(url);for(int i=0 ;iif(page.substring(i,i+goal.length()).equals(goal))
System.out.print
(page.substring(i+goal.length()+1,i+goal.length()+len+1));
}
public static String get(String url) throws IOException
{
URL URL = new URL(url);
HttpURLConnection http = (HttpURLConnection) URL.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader in = new BufferedReader
(new InputStreamReader(http.getInputStream(),"utf-8"));
String line = in.readLine();
while (line != null)
{
sb.append(line); line = in.readLine();
}
return sb.toString();
}
}
留言
張貼留言