Re: [問題] 如何取得html標籤中的值??

看板java作者時間19年前 (2006/10/10 22:16), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
httpunit 也很好用喔!! http://httpunit.sourceforge.net/ try it!!! ※ 引述《feicsh.bbs@bbs.wretch.cc (飛)》之銘言: : Pattern pattern = Pattern.compile("href=\"[^\"]*"); : Matcher m = pattern.matcher(sb.toString()); : while(m.find()) { : String str = m.group(); : str = str.replaceFirst("href=\"", ""); : str = str.substring(0, str.length()-1); : System.out.println(str); : } : Instead of regular expression, : HTML parser might be the more appropriate and pretty solution. : If you are interested, read this site http://htmlparser.sourceforge.net/ : ※ 引述《keyairotika.bbs@ptt.cc (空白之才)》之銘言: : > import java.util.regex.*; : > import java.io.*; : > public class Test1 { : > public static void main(String[] args) throws Exception { : > StringBuffer sb = new StringBuffer(); : > BufferedReader br = new BufferedReader(new FileReader("c:/work/112.txt")); : > String line=""; : > while( (line=br.readLine()) != null) { : > sb.append(line); : > } : > Pattern pattern = Pattern.compile(" "); //裡面要怎麼用才能把我要的網址抽出來? : > Matcher m = pattern.matcher(sb.toString()); : > while(m.find()) { : > String str = m.group(); : > System.out.println(str); : > } : > } : > } : > 假設在infile.txt裡面有內容如下 : > <a class=l href="http://www.ntnu.edu.tw/art/" onmousedown=" : > <a class=l href="http://203.71.53.40/" onmousedown=" : > <a class=l href="http://www.scdxart.com/" onmousedown="return : > 我只想要裡面的網址如下 : > http://www.ntnu.edu.tw/art/ : > http://203.71.53.40/ : > http://www.scdxart.com/ : > Pattern pattern = Pattern.compile(" "); : > 這行裡面要怎麼用 : > 我現在只弄到這樣 但還是不對 : > Pattern pattern = Pattern.compile("http://[^\"\\s]+"); -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.236.102.99
文章代碼(AID): #15Awiow5 (java)
文章代碼(AID): #15Awiow5 (java)