等待页面加载,然后再从 Java 中的 URL 读取内容
我正在从 java 代码中的 url 读取内容,但我想要读取的页面在加载时执行命令,并且 InputStreamReader 在页面完全加载之前读取页面,因此我的缓冲阅读器仅在实际内容之前收集页面上的 HTML已加载。
我的主要目标是在页面上找到“销售”一词,但如果在加载整个页面之前连接打开的流,我将无法执行此操作。有没有办法等待它加载或其他什么?
这是我的代码:
URL url = new URL("http://urlgoeshere.com?"+ withAParam);
URLConnection uc = url.openConnection();
uc.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String inputLine = in.readLine();
int index = -1;
while ((inputLine = in.readLine()) != null){
index=inputLine.toLowerCase().indexOf("sales");
if(index>=0){
log.info("Found sales!");
break;
}
}
if (in != null){
in.close();
}
I am reading from a url in my java code but the page I want to read executes a command when loaded and the InputStreamReader reads the page before it has completely loaded, so my buffered reader only collects the HTML on the page before the real content is loaded.
My main goal is to find the word "sales" on the page, but I can't do this if the stream opened is connected before the full page is loaded. Is there a way to wait for it to load or something?
Here is my code:
URL url = new URL("http://urlgoeshere.com?"+ withAParam);
URLConnection uc = url.openConnection();
uc.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String inputLine = in.readLine();
int index = -1;
while ((inputLine = in.readLine()) != null){
index=inputLine.toLowerCase().indexOf("sales");
if(index>=0){
log.info("Found sales!");
break;
}
}
if (in != null){
in.close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在首先介绍一些无法解决您的问题的 Java 编码技巧,然后介绍可以解决您的问题的技巧。
您应该重构您的代码并使用
try-finally
关闭finally 块中的流,以确保即使抛出异常它也始终关闭。那么我不会将
indexOf
与 int 一起使用。为了使代码更加清晰、可读且不那么冗长,请编写if(inputLine.toLowerCase().contains("sales")){
直接在 if 语句中并删除所有索引代码。
您可以尝试 apache API http://hc.apache.org/httpcomponents-client -ga/index.html 获取主页。
Now first some Java coding tips that won't solve your problem then a tip that may.
You should refactor your code and use
try-finally
where you close the stream in finally block to make sure it always closes even when an exception is thrown.Then I wouldn't use the
indexOf
with an int. To make the code more sharp,readable and less verbose writeif(inputLine.toLowerCase().contains("sales")){
directly in your if statement and remove all index code.
You can try the apache API http://hc.apache.org/httpcomponents-client-ga/index.html to fetch the homepage.