如何让Java应用程序与网站交互

我有一个程序从excel文件获取数据并为用户操作它.但是为了获得excel文件的更新,需要从网站下载.我最初尝试使用机器人类导航到网站,使用用户名和密码登录,然后导航到网站的正确部分,找到“下载excel电子表格”按钮并单击它.但我明白这是一种可怕的方式,它并不总是有效.
有什么更好的方法我可以这样做,以便我的程序可以访问网站并导航到我想要的页面,然后下载数据.我读到了关于“页面报废”的内容,但我认为不会允许我这样做.我真的想与网页互动,而不是下载它的内容.任何帮助都会很棒.
谢谢,
彼得

解决方法

如果您确实需要与网站互动,那么selenium / webdriver非常适合您的需求:

http://code.google.com/p/selenium/wiki/GettingStarted

Google搜索示例:

package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Example  {
    public static void main(String[] args) {
        // Create a new instance of the html unit driver
        // Notice that the remainder of the code relies on the interface,// not the implementation.
        WebDriver driver = new HtmlUnitDriver();

        // And Now use this to visit Google
        driver.get("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
    }
}

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...