java - 如何用Java与网页交互?

标签 java http html interaction

现在,我是 Java 的初学者。我以某种方式设法理解了以下代码。

 import java.net.*;
  import java.io.*;

 public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

我实际上花了相当多的努力才做到这一点,所以请理解我是一个初学者。我想与网页进行交互。从代码中我了解到网页的任何信息都会被简单地显示出来。我只是需要你的帮助来建议我接下来应该学习什么。

我要我的网页 转到该网页,登录 然后点击一个按钮 比较两列,如果不相等则报告给我。

我确实在 HTTP 上进行了阅读,并且我知道网络交互是可能的。 人们的代码超出了我的理解范围。

我对继承或封装不太了解[仍在学习](以防需要)

使用我提供的代码,是否可以添加我的要求? 因为人们给出的是 1 行代码或 30 行代码...请意识到我不太擅长。

我确实做了研究。我只是希望有人给我指导。由于方法太多,我开始感到困惑。

我想有人告诉我 php 在这件事上更容易,但在 php 中我什至什么都不知道。 (我知道这是 OOPS 语言,但仍然如此)

我们非常感谢任何形式的指导。

最佳答案

如果您确实需要与网站交互,那么 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/21286746/

相关文章:

java - Bean 验证组序列不起作用

javascript - JS Canvas 中具有随机颜色的同心圆(带循环)?

javascript - 通过升级保存 HTML5 Canvas

html - 单击图像正在缩放而不是显示叠加图像

http - Grinder - 客户端打开连接但未发送任何字节

http - Go 服务器中并发(同时)HTTP 连接的理论最大数量是多少?

java - -bash : !“:尝试运行Java程序时未找到事件

java - 使用 JDBC 从 Oracle 获取唯一索引的所有外键

java - 从 C++ 套接字读取 Java 中的数据

php - HTML - 包含特殊字符时不发送表单字段