使用Java在网页中的许多可能选项中选择一个
我想在以下步骤后获取以下网页()的HTML代码:使用Java在网页中的许多可能选项中选择一个,java,html,Java,Html,我想在以下步骤后获取以下网页()的HTML代码: 在Facoltá中选择“Dipartmento di Informatica” 选择“Informatica”(或其他可用选项之一) 点击“Avvia Ricerca” 我不是很热衷于这件事,但我注意到页面的URL在每次选择后都保持不变 有谁能帮我描述一下,可能是细节,我该怎么做?不幸的是,我不是网络编程专家 非常感谢经过一些测试后,它会用POST请求刷新页面 fac_id:1012 -- cds_id:197 -- ad_id: -- At
非常感谢经过一些测试后,它会用POST请求刷新页面
fac_id:1012 --
cds_id:197 --
ad_id: -- Attività didattica
docente_id: -- Id of the docent selected
data:06/03/2014 -- Date
无论如何,您忽略了Attivitáditattica
、Docente
和数据esame
只需使用(?)和这个POST参数运行HTTP请求,并使用XML解析器读取tplmessage表的输出
请针对HTTP请求尝试此教程:
尝试阅读以下内容以了解如何解析响应:
使用本教程代码的示例如下:
HttpURLConnection connection = null;
try
{
URL url = new URL("http://www.studenti.ict.uniba.it/esse3/ListaAppelliOfferta.do");
connection = (HttpURLConnection) url.openConnection(); // open the connection with the url
String params =
"fac_id=1012&cds_id=197"; // You need to add ad_id, docente_id and data
connection.setRequestMethod("POST"); // i need to use POST request method
connection.setRequestProperty("Content-Length", "" + Integer.toString(params.getBytes().length)); // It will add the length of params
connection.setRequestProperty("Content-Language", "it-IT"); // language italian
connection.setUseCaches (false);
connection.setDoInput (true);
connection.setDoOutput (true);
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream ());
wr.writeBytes (params); // pass params
wr.flush (); // send request
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuilder response = new StringBuilder();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
}
catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
finally
{
// close connection if created
if (connection != null)
connection.disconnect();
}
在response
中,您将拥有页面的DOM
无论如何,请使用Chrome developers工具获取请求参数:
谢谢马可,你的帮助非常有用。我设法获得了包含表的页面的HTML代码,我只需要修改POST请求的一些参数。例如字符串params=“fac\u id=1012&cds\u id=1102&ad\u id=&docente\u id=&data=&btnSubmit=Avvia Ricerca”;允许获取包含关于Corso di Studio=1102考试的所有信息的表格。我没有添加任何广告id、数据或文档id,因为我对获取所有考试感兴趣。btn=Submit=Avvia Ricerca完成了剩下的工作,将这些数据显示在网页的表格中。