【发布时间】:2014-02-26 13:58:21
【问题描述】:
我正在尝试从 URL 中提取网页内容。我已经编写了代码,但我认为我在正则表达式部分犯了一个错误。当我运行代码时,只有第一行出现在控制台中。我正在使用NetBeans。我已经拥有的代码:
private static String text;
public static void main(String[]args){
URL u;
InputStream is = null;
DataInputStream dis;
String s;
try {
u = new URL("http://ghr.nlm.nih.gov/gene/AKT1 ");
is = u.openStream();
dis = new DataInputStream(new BufferedInputStream(is));
text="";
while ((s = dis.readLine()) != null) {
text+=s;
}
} catch (MalformedURLException mue) {
System.out.println("Ouch - a MalformedURLException happened.");
mue.printStackTrace();
System.exit(1);
} catch (IOException ioe) {
System.out.println("Oops- an IOException happened.");
ioe.printStackTrace();
System.exit(1);
} finally {
String pattern = "(?i)(<P>)(.+?)";
System.out.println(text.split(pattern)[1]);
try {
is.close();
} catch (IOException ioe) {
}
}
}
}
【问题讨论】:
-
强制 不鼓励使用正则表达式来解析 html,请使用 html 解析 API,例如 jsoup 注释