Java--HTML客户端在完成页面加载之前返回响应
我必须从网页上阅读某个领域的内容。有人告诉我,我需要获取整个页面,然后从html内容中提取文本。 我使用以下程序来获取所需的页面html内容。 现在的问题是,这个网页需要几秒钟的时间来加载我想要读取的实际文本值,即使其他静态页面组件都是在前面加载的。我的程序在静态组件加载之后,但在我的值加载之前,返回html内容。因此,我得到的最终HTML具有页面加载过程pic,而不是实际值。 有没有人可以指导我在这个程序中所需的更改,这将有助于它等到页面完全加载Java--HTML客户端在完成页面加载之前返回响应,java,httpclient,httpresponse,Java,Httpclient,Httpresponse,我必须从网页上阅读某个领域的内容。有人告诉我,我需要获取整个页面,然后从html内容中提取文本。 我使用以下程序来获取所需的页面html内容。 现在的问题是,这个网页需要几秒钟的时间来加载我想要读取的实际文本值,即使其他静态页面组件都是在前面加载的。我的程序在静态组件加载之后,但在我的值加载之前,返回html内容。因此,我得到的最终HTML具有页面加载过程pic,而不是实际值。 有没有人可以指导我在这个程序中所需的更改,这将有助于它等到页面完全加载 HttpPost post = new Htt
HttpPost post = new HttpPost("https://..../login");
//prepare get method
HttpGet httpget = new HttpGet("https://...../value#/123");
// add parameters to the post method
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", "<name>"));
parameters.add(new BasicNameValuePair("password", "<password>"));
try {
UrlEncodedFormEntity sendEntity = new UrlEncodedFormEntity(parameters, HTTP.DEF_CONTENT_CHARSET);
post.setEntity(sendEntity);
// create the client and execute the post method
HttpClient client = HttpClientBuilder.create().build();
HttpResponse postResponse = client.execute(post);
System.out.println("Statusline: " + postResponse.getStatusLine());
//Output the Response from the POST
System.out.println(getStringFromInputStream(postResponse.getEntity().getContent()));
//releasing POST
EntityUtils.consume(postResponse.getEntity());
//Execute get
HttpContext context = new BasicHttpContext();
HttpResponse getResponse = client.execute(httpget);//, context);
System.out.println("Statusline: " + getResponse.getStatusLine());
if (getResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
throw new IOException(getResponse.getStatusLine().toString());
System.out.print(getStringFromInputStream(getResponse.getEntity().getContent()));
HttpPost=新的HttpPost(“https://..../login");
//准备获取方法
HttpGet HttpGet=新的HttpGet(“https://...../value#/123");
//向post方法添加参数
列表参数=新的ArrayList();
添加(新的BasicNameValuePair(“用户名”,“参数”);
添加(新的BasicNameValuePair(“密码”,“密码”));
试一试{
UrlEncodedFormEntity sendEntity=新的UrlEncodedFormEntity(参数,HTTP.DEF_CONTENT_CHARSET);
post.setEntity(sendEntity);
//创建客户机并执行post方法
HttpClient client=HttpClientBuilder.create().build();
HttpResponse postResponse=client.execute(post);
System.out.println(“Statusline:+postResponse.getStatusLine());
//从POST输出响应
System.out.println(getStringFromInputStream(postResponse.getEntity().getContent());
//释放柱
EntityUtils.consume(postResponse.getEntity());
//执行get
HttpContext=新的BasicHttpContext();
HttpResponse getResponse=client.execute(httpget);/,context);
System.out.println(“Statusline:+getResponse.getStatusLine());
如果(getResponse.getStatusLine().getStatusCode()!=HttpStatus.SC_OK)
抛出新IOException(getResponse.getStatusLine().toString());
System.out.print(getStringFromInputStream(getResponse.getEntity().getContent());
您也可以使用Jsoup库
访问您正在加载一个包含ajax(javascript)的页面并在客户端完成。因此,您也需要解析javascript。我建议使用“Selenium”包完成此类任务。很抱歉之前没有响应。您的建议有效,非常感谢!JSOUP可以在JS评估后加载网站。不确定人们为什么会给出负面评论