从具有登录要求Java的网页获取数据

时间:2014-10-11 07:16:20

标签: java login htmlunit

所以最近我决定自学如何从网页上获取数据。我设法从不同的网页上获取JSON的数据,但是当我尝试从该网站复制所有内容时,它并没有显示我实际需要的数据。

我正在尝试的页面例如:http://www.tremorgames.com/index.php?action=shop&page=2(您可能需要注册)。我想要获得的数据是游戏名称/价格或股票,如果我能得到一个,那么我将能够得到所有。

问题在于Dev工具显示了代码,但是当我尝试使用Java将所有内容复制到文件时,它并没有显示代码的大部分内容。

(我也尝试使用Jsoup,它也不起作用)。 这就是我从网页上复制的内容:

BufferedReader reader = null;
try {
    URL url = new URL("http://www.tremorgames.com/index.php?action=shop&page=2");
    reader = new BufferedReader(new InputStreamReader(url.openStream()));
    StringBuffer buffer = new StringBuffer();
    int read;
    char[] chars = new char[1024];
    while ((read = reader.read(chars)) != -1)
        buffer.append(chars, 0, read); 

    return buffer.toString();
} finally {
    if (reader != null)
        reader.close();
}

正如我所说,我正在努力学习所以任何指针都是受欢迎的(我一直在寻找一段时间,直到我放弃并编写其余的代码)。

提前致谢。

1 个答案:

答案 0 :(得分:3)

好的,我刚才完成了这个,但忘了回答我自己的问题。 我之所以使用HtmlUnit是因为它看起来最简单。

import com.gargoylesoftware.htmlunit.WebClient;  
import com.gargoylesoftware.htmlunit.html.HtmlInput;  
import com.gargoylesoftware.htmlunit.html.HtmlPage;  
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

为了从某个网页获取数据,我需要先登录网站。为此,我需要启动一个Web客户端。 需要记住的是需要使用相同的Web客户端,因此您需要在调用login方法的方法中启动WebClient(此方法稍后也会发送WebClient以获取数据以及您可能需要的任何其他内容)。

WebClient webClient = new WebClient(); //Initiate a WebClient variable.  
webClient = tremorLogin(webClient);

然后在tremorLogin中我将登录网站并将客户端返回给webClient变量。

//Login into Tremor Games and return the client(Saves the cookies).
private static WebClient tremorLogin(WebClient webClient) throws Exception
{
    webClient.getOptions().setJavaScriptEnabled(false);
    HtmlPage currentPage = webClient.getPage("http://www.tremorgames.com/"); //Load page at the STRING address.
    HtmlInput username = currentPage.getElementByName("loginuser"); //Find element called loginuser for username
    username.setValueAttribute(user); //Set value for username
    HtmlInput password = currentPage.getElementByName("loginpassword"); //Find element called loginpassword for password
    password.setValueAttribute(pass); //Set value for password
    HtmlSubmitInput submitBtn = currentPage.getElementByName("Submit"); //Find element called Submit to submit form.
    currentPage = submitBtn.click(); //Click on the button.

    return webClient;
}

登录用户文本是检查网站源代码时调用用户名的文本字段的内容。

HtmlInput username = currentPage.getElementByName("loginuser");

登录密码文本是检查网站源代码时调用密码的文本字段。

HtmlInput password = currentPage.getElementByName("loginpassword");

user是您的用户名(String type),pass是您的密码(String type)

username.setValueAttribute(user);  
password.setValueAttribute(pass);

在写完用户名和密码后,您需要点击提交按钮,为此您需要在网站的源代码中找到该按钮的名称(与用户名和密码文本字段相同。找到后)按钮的名称,你需要点击它,这是第二行。

 HtmlSubmitInput submitBtn = currentPage.getElementByName("Submit"); //Find element called Submit to submit form.
currentPage = submitBtn.click(); //Click on the button.

一旦您返回此页面,您的Web客户端将以原始方法保存,之后您可以从那里获取所有数据或您可能希望从网站获得的任何其他内容。 在原始方法中,您可能会有类似

的内容
HtmlPage currentPage = webClient.getPage("http://www.tremorgames.com/index.php?action=shop&searchterm=steam&search_category=5&sort=price_asc&page=1");
String pageSource = currentPage.asXml();

在pageSource中将网站设为xml之后,您将拥有与开发人员工具中看到的文本/代码完全相同的文本/代码,之后您只需要在其中搜索所需的数据。

希望这有助于为人们节省时间。