gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:51:47 26 4
gpt4 key购买 nike

我有一个程序可以从 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());
}
}

关于java - 如何让 Java 应用程序与网站交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4640972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com