加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 00:00:02 所属栏目:Java 来源:网络整理
导读:我有一个程序从excel文件中获取数据并为用户操作它.但是为了获得excel文件的更新,需要从网站下载.我最初尝试使用机器人类导航到网站,使用用户名和密码登录,然后导航到网站的正确部分,找到“下载excel电子表格”按钮并单击它.但我明白这是一种可怕的方式,它并
我有一个程序从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());
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读