當前位置:編程學習大全網 - 源碼下載 - 如何獲取網頁源代碼中標簽信息的內容

如何獲取網頁源代碼中標簽信息的內容

樓主可以試試我寫的這個例子:

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.URISyntaxException;

import java.net.URL;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class URLTest {

/**

* @param args

* @throws URISyntaxException

*/

public static void main(String[] args) throws Exception {

URL url = new URL("/");

InputStreamReader reader = new InputStreamReader(url.openStream());

BufferedReader br = new BufferedReader(reader);

String s = null;

while((s=br.readLine())!=null){

s = GetContent(s);

if(s!=null){

System.out.println(s);

}

}

br.close();

reader.close();

}

public static String GetContent(String html) {

//String html = "<ul><li>1.hehe</li><li>2.hi</li><li>3.hei</li></ul>";

String ss = ">[^<]+<";

String temp = null;

Pattern pa = Pattern.compile(ss);

Matcher ma = null;

ma = pa.matcher(html);

while(ma.find()){

temp = ma.group();

if(temp!=null){

if(temp.startsWith(">")){

temp = temp.substring(1);

}

if(temp.endsWith("<")){

temp = temp.substring(0, temp.length()-1);

}

if(!temp.equalsIgnoreCase("")){

//System.out.println(temp);

return temp;

}

}

}

return null;

}

}

  • 上一篇:網站域名如何申請虛擬空間,如何購買和備案,完成後如何維護~!如何讓百度谷歌收錄
  • 下一篇:背景差分法的常用的背景建模方法
  • copyright 2024編程學習大全網