微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何让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());
    }
}

原文地址:https://www.jb51.cc/java/127013.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐