java中,如何提取网页的指定内容
比如网页的源代码中出现多次<class>123<br><class>acx<br><class>a12a<br>如何用java能够提取到123、acx、a12a这内容,并...
比如网页的源代码中出现多次
<class>123<br>
<class>acx<br>
<class>a12a<br>
如何用java能够提取到123、acx、a12a这内容,并写入在txt文件中。麻烦大神写个范例吧 展开
<class>123<br>
<class>acx<br>
<class>a12a<br>
如何用java能够提取到123、acx、a12a这内容,并写入在txt文件中。麻烦大神写个范例吧 展开
1个回答
展开全部
FileReader fr = new FileReader("*.htm"); FileWriter fw = new FileWriter("E:/a.txt");
char[] c = new char[1024];
StringBuffer sb = new StringBuffer();
int n;
while ((n = fr.read(c)) != -1) {
sb.append(c, 0, n);
}
fr.close();
Pattern p = Pattern.compile("<class>.*?<br>");
Matcher m = p.matcher(sb);
while (m.find()) {
String tmp = m.group();
System.out.println(tmp);
System.out.println(tmp.substring(7, tmp.length() - 4));
fw.write(tmp.substring(7, tmp.length() - 4));
}
fw.close();
char[] c = new char[1024];
StringBuffer sb = new StringBuffer();
int n;
while ((n = fr.read(c)) != -1) {
sb.append(c, 0, n);
}
fr.close();
Pattern p = Pattern.compile("<class>.*?<br>");
Matcher m = p.matcher(sb);
while (m.find()) {
String tmp = m.group();
System.out.println(tmp);
System.out.println(tmp.substring(7, tmp.length() - 4));
fw.write(tmp.substring(7, tmp.length() - 4));
}
fw.close();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询