Google App Engine ( Java ):URL 提取响应太大问题
我正在尝试在谷歌应用程序上构建某种网络服务。
现在的问题是,我需要从网站获取数据(HTML 抓取)。
该请求如下所示:
URL url = new URL(p_url);
con = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(con.getInputStream());
BufferedReader reader = new BufferedReader(in);
String result = "";
String line = "";
while((line = reader.readLine()) != null)
{
System.out.println(line);
}
return result;
现在 App Engine 在第 3 行给出了以下异常:
com.google.appengine.api.urlfetch.ResponseTooLargeException
这是因为最大请求限制为 1mb,而页面的总 HTML 约为 1.5mb。
现在我的问题是: 我只需要抓取 html 的前 20 行。有没有办法只获取 HTML 的一部分,这样就不会抛出 ResponseTooLargeException ?
提前致谢!
I'm trying to build some sort of webservice on google apps.
Now the problem is, I need to get data from a website (HTML Scraping).
The request looks like :
URL url = new URL(p_url);
con = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(con.getInputStream());
BufferedReader reader = new BufferedReader(in);
String result = "";
String line = "";
while((line = reader.readLine()) != null)
{
System.out.println(line);
}
return result;
Now App Engine gives me the follwing exception at the 3th line:
com.google.appengine.api.urlfetch.ResponseTooLargeException
This is because the maximum request limit is at 1mb and the total HTML from the page is about 1.5mb.
Now my question:
I only need the first 20 lines of the html to scrape. Is there a way to only get a part of the HTML so that the ResponseTooLargeException will not be thrown?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用低级 URLFetch api 解决了该问题。
并将allowtruncate选项设置为true;
http ://code.google.com/intl/nl-NL/appengine/docs/java/javadoc/com/google/appengine/api/urlfetch/FetchOptions.html
基本上它的工作原理如下:
Solved the problem by using the low level URLFetch api.
And setting the allowtruncate option to true;
http://code.google.com/intl/nl-NL/appengine/docs/java/javadoc/com/google/appengine/api/urlfetch/FetchOptions.html
Basicly it works like this :