如何用Java与网页交互?
现在,我是Java的初学者。我设法理解了下面的代码如何用Java与网页交互?,java,http,webpage,interaction,Java,Http,Webpage,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 = yaho
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,我知道网络交互是可能的。
人们有我无法理解的代码
我对继承或封装不太了解[仍在学习](如果需要的话)
使用我提供的代码,是否可以添加我的需求?
因为人们给出了一行代码或30行代码…请意识到我在这方面不是很好
我做过研究,我只是想有人给我指路。由于方法太多,我开始感到困惑
我想有人告诉我php在这个问题上更容易,但在php中我甚至什么都不知道。(我知道这是OOPS语言,但仍然)
我们真诚地感谢您提供的任何指导。如果您确实需要与网站互动,那么selenium/webdriver非常适合您的需要: 谷歌搜索示例:
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());
}
}
哦,是的,我知道它是完美的!我在软件工程三年级的朋友很遗憾地告诉我,他说可能需要2个月的时间来理解这一点……我花了大约两个月的时间来开发这段代码!有可能在两周内学会吗?是的,绝对有可能在两周内学会。其次,它被广泛用于自动化测试,因此值得学习Selenium,您还将学习其他东西,如XML、xPath等。它看起来相当有趣……您可以使用Selenium登录吗?比如,如果我得到一个网页,像facebook一样,它会允许我输入用户名或密码等详细信息吗?是的,Selenium的构建几乎可以让用户在网页上做任何事情。它现在是业界最好的测试框架之一,登录是任何应用程序的初始和基本测试点。