我正在下载 website 的搜索结果使用java,这对于首页来说效果很好。
URL url = new URL(
"http://www.geocaching.com/seek/nearest.aspx?country_id=79&as=1&ex=0");
BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
但是我怎样才能得到结果的第二页呢?指向第二页的链接使用 JavaScript 调用 javascript:__doPostBack('ctl00$ContentBody$pgrBottom$lbGoToPage_2','')
,它调用以下函数:
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
我想,我需要从我的java程序中调用该函数来获取第二页,但是如何?
最佳答案
据我所知,您想要执行 __doPostBack(...) ,就像它在浏览器中执行的网页上一样。我不明白如何使用 Java 以编程方式做到这一点。如果我是你,我会尝试手动创建 Javascript 调用的 post 请求(可能使用类似 this 的东西)。我知道它并不理想,因为如果页面发生更改,您的应用程序将不再工作,但这是我能想到的唯一合理的解决方案。
请问您到底想要实现什么目标?会像 Selenium有帮助吗?
关于java - 从 Java 调用 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6451570/