Perl能够很好提取网页中的信息,那么Java也能象它一样强大吗?
如果给定一个URL,是不是Java会有特定的方法直接就获得到页面的内容了?
使用java会很麻烦吗?
希望各位多多指点
如果给定一个URL,是不是Java会有特定的方法直接就获得到页面的内容了?
使用java会很麻烦吗?
希望各位多多指点
解决方案 »
- 大牛,高手都来..略显复杂
- HttpClient用法
- Oracle与Tomcat的问题,oracle监听自己停止
- 十万火急!!用C/C++写了个动态链接库加密库,用JNI调用,LoadLibrary报 " undefined symbol: _ZNSt8ios_base4InitC1Ev"异常 ??
- 求助:检索PDF文件并打印的问题?
- 使用eclipse过程中的疑问...深入关于java进程控制
- 一段JScript,想在java中实现。
- 请教:关于null是否为关键字的问题
- tcp套接字问题!
- 怎样在linux下用exec启动gnome-terminal,并向其发出ls等命令?
- 关于java应用程序定时启动的问题
- 回文问题的算法
to apple21 :今天刚买了本《java编程思想》,好好看看去,
// Create a URL for the desired page
URL url = new URL("http://www.google.com");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
port = "9808",
username = "mxl",
password = "mxlcaca";
Authenticator.setDefault(new SimpleAuthenticator(
username,password));
Properties systemProperties = System.getProperties(); systemProperties.setProperty("http.proxyHost",proxy);
systemProperties.setProperty("http.proxyPort",port); currentUrl=new URL(url);
URLConnection con=currentUrl.openConnection();
HttpURLConnection httpcon=(HttpURLConnection) con;
con.connect();
System.out.println(httpcon.getResponseCode());
if(httpcon.getResponseCode()==HttpURLConnection.HTTP_OK){
if(httpcon.getContentType().startsWith("text/html")){
InputStreamReader in=new InputStreamReader(con.getInputStream());
BufferedReader buf=new BufferedReader(in);
InputStream ins=con.getInputStream();
int c; while (((c=ins.read())!=-1))
{
System.out.print((char)c);
}
buf.close();
in.close();
System.getProperties().put("proxySet","true");//你少了这句,加上就OK
System.getProperties().setProperty( "http.proxyHost","代理地址");
System.getProperties().setProperty( "http.proxyPort","代理端口");
现在我想问问,我从网页中获得内容放在BufferedRead中了,如果使用正则表达式抽取需要的数据,是直接对BufferedRead进行操作吗?循环调用它的readLine()是不是太慢了?有没有更好的办法可以一次就可以找出来的?
可以用char[]来读,一次读取一块,块大小可以自己定义。
正则表达式抽取所需要的数据,
如果html本身是格式良好,可以借鉴htmlparser或者jbuilder的http unit之类,
如果格式不是良好的,可能就只有自己写正则表达式对抽取出来的数据进行操作。最好是所有数据都读完了,得到一个完整的String进行操作。
对str进行操作,只是一部分。也不知道有没有说清楚……呵呵
我用了readLine(),能够解决问题,我想char[]也是一个好办法,我也尝试了htmlparser,不过不知道怎么用它,而且感觉我要实现的功能应该都是不太复杂的,所以就没用了。
使用char[]主要是不能确定大小,担心有的重要信息会因此被分割开,从而就找不到了