我想获取以下网页( http://www.studenti.ict.uniba.it/esse3/ListaAppelliOfferta.do )的 HTML 代码:
- 在 Facoltà 中选择“Dipartimento di Informatica”
- 选择“Informatica”(或其他可用选项之一)
- 点击“Avvia Ricerca”
我对此事不太感兴趣,但我注意到每次选择后页面的 URL 都保持不变!?!
任何人都可以帮助描述(可能是详细的)我该怎么做吗?不幸的是我不是网络编程方面的专家。
非常感谢
最佳答案
经过一些测试,它通过 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
和 Data esame
的值(value)
只需使用 HttpURLConnection
运行 HTTP 请求(?) 使用此 POST 参数,并使用 XML 解析器读取 tplmessage 表的输出。
尝试使用本教程来处理 HTTP 请求:click .
尝试阅读此内容以了解如何解析响应:click
<小时/>使用教程代码的示例:
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 开发者工具来获取请求参数:
关于java - 使用 Java 在网页中的众多可能选项中选择一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22441565/