如何在Java中与网页交互? [英] How to interaction with a webpage in Java?
问题描述
现在,我是Java中的初学者。我以某种方式设法理解以下代码。
Now, I am a beginner in Java. I have somehow managed to understand the following code.
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();
}
}
我实际上付出了相当大的努力,所以请做明白我是初学者。我想与网页互动。从代码中,我明白了网页的任何信息都会被简单地显示出来。我只是需要你的帮助来建议我,我应该接下来学习。
I actually got that with quite some effort so please do understand I am a beginner. I want to interact with the webpage. From the code, I understood whatever information of the webpage will be simply displayed. I just need your help to advice me wha should I study next.
我希望我的网页进入网页,登录
然后点击按钮
在两列之间进行比较,如果不相等则报告给我。
I want my webpage to go to the webpage, login Then click on a button Do a comparision between two columns and report it to me if it isn't equal.
我确实在HTTP上阅读,我知道可以进行webinteraction。
人们的代码超出了我的理解。
I did do reading on HTTP and I know webinteraction is possible. People have code out there that is above my understanding.
我对遗传或封装知之甚少[仍在学习](如果需要)
I don't know much about inheritence or encapsulation [still learning](incase it is required)
使用我提供的代码,是否可以添加我的要求?
因为人们正在给出一个带衬里的代码或30行代码...请注意我不是很好。
Using the code I provided, is it possible to add on my requirements? Cause people are giving one lined codes or 30 lined code...please realize I am not so good in it.
我做了研究。我只是希望有人给我指导。由于有这么多方法,我开始感到困惑。
I did do research .I just want someone to give me direction. I am starting to get confused due to so many methods.
我想有人告诉我php在这件事上更容易,但在php中我甚至都不知道。 (我知道它是OOPS语言,但仍然)
I think someone told me php is easier on this matter, but in php I don't even know anything. (I know it is OOPS language but still)
真正赞赏任何形式的指导。
Any sort of guidance is truely appreciated.
推荐答案
如果您确实需要与网站互动,那么selenium / webdriver非常适合您的需求:
If you actually need to interact with the website then selenium/webdriver is perfect for your needs:
http://code.google.com/p/selenium/wiki/GettingStarted
Google搜索示例:
Sample Google search:
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中与网页交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!