获取网页内容到String很慢
我使用 HttpURLConnection.getInputStream() 下载了一个网页,并将内容获取到字符串,我执行以下方法:
String content="";
isr = new InputStreamReader(pageContent);
br = new BufferedReader(isr);
try {
do {
line = br.readLine();
content += line;
} while (line != null);
return content;
} catch (Exception e) {
System.out.println("Error: " + e);
return null;
}
页面的下载速度很快,但将内容获取到字符串的处理非常慢。还有另一种方法可以更快地将内容获取到字符串吗?
我将其转换为字符串以插入数据库中。
I did the download a web page with the HttpURLConnection.getInputStream() and to get the content to a String, i do the following method:
String content="";
isr = new InputStreamReader(pageContent);
br = new BufferedReader(isr);
try {
do {
line = br.readLine();
content += line;
} while (line != null);
return content;
} catch (Exception e) {
System.out.println("Error: " + e);
return null;
}
The download of the page is fast, but the processing to get the content to String is very slow. There is another way faster to get the content to a String?
I transform it to String to insert in the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
按字节数读入缓冲区,而不是像行那样任意的东西。仅此一点就应该是加快速度的良好开端,因为读者不必找到行尾。
Read into buffer by number of bytes, not something arbitrary like lines. That alone should be a good start to speeding this up, as the reader will not have to find the line end.
使用
StringBuffer
< /a> 相反。编辑一个例子:
Use a
StringBuffer
instead.Edit for an example:
使用 blob/clob 将内容直接放入数据库。
逐行构建字符串并将其放入数据库的任何具体原因?
use the blob/clob to put the content directly into database.
any specific reason for buliding string line by line and put it in the database??
我使用jsoup来获取页面的指定内容,这里是一个基于jquery和jsoup的Web演示,用于捕获网页的任何内容,您应该为需要捕获的页面内容指定ID或Class:http://www.gbin1.com/technology/democenter/20120720jsoupjquerysnatchpage/index.html
I'm using jsoup to get specified content of a page and here is a web demo based on jquery and jsoup to catch any content of a web page, you should specify the ID or Class for the page content you need to catch: http://www.gbin1.com/technology/democenter/20120720jsoupjquerysnatchpage/index.html