Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从具有登录要求的网页获取数据_Java_Login_Htmlunit - Fatal编程技术网

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

Java 从具有登录要求的网页获取数据,java,login,htmlunit,Java,Login,Htmlunit,所以最近我决定自学如何从网页上获取数据。我设法从另一个网页从JSON中获取数据,但当我试图从这个网站复制所有内容时,它并没有显示我实际需要的数据 我正在尝试的页面是例如:(您可能需要注册)。我试图获得的数据是例如游戏名称/价格或股票,如果我能获得一个,那么我将能够获得所有数据 问题是,Dev工具显示了代码,但当我尝试使用Java将所有内容复制到文件时,它并没有显示大部分代码 (我也尝试过Jsoup,但它也不起作用)。 这是我从网页复制的工具: BufferedReader reader = nu

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

我正在尝试的页面是例如:(您可能需要注册)。我试图获得的数据是例如游戏名称/价格或股票,如果我能获得一个,那么我将能够获得所有数据

问题是,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();
}
正如我所说,我正在努力学习,所以任何指针都是受欢迎的(我已经搜索了一段时间,直到我放弃并编写了其余的代码)


提前谢谢。

好的,我刚才完成了,但忘了回答我自己的问题。 我用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以获取数据和您可能需要的任何其他内容)

然后在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;
}
loginuser文本是在检查网站源代码时调用用户名的文本字段

HtmlInput username = currentPage.getElementByName("loginuser");
HtmlInput password = currentPage.getElementByName("loginpassword");
loginpassword文本是在检查网站源代码时调用的密码文本字段

HtmlInput username = currentPage.getElementByName("loginuser");
HtmlInput password = currentPage.getElementByName("loginpassword");
user是您的用户名(字符串类型),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形式创建网站后,您将获得与在开发人员工具中看到的完全相同的文本/代码,稍后您只需在其中搜索所需的数据


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

好的,所以我刚才完成了这篇文章,但忘了回答我自己的问题。 我用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以获取数据和您可能需要的任何其他内容)

然后在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;
}
loginuser文本是在检查网站源代码时调用用户名的文本字段

HtmlInput username = currentPage.getElementByName("loginuser");
HtmlInput password = currentPage.getElementByName("loginpassword");
loginpassword文本是在检查网站源代码时调用的密码文本字段

HtmlInput username = currentPage.getElementByName("loginuser");
HtmlInput password = currentPage.getElementByName("loginpassword");
user是您的用户名(字符串类型),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形式创建网站后,您将获得与在开发人员工具中看到的完全相同的文本/代码,稍后您只需在其中搜索所需的数据


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

可能的重复正如我所说的,我已经搜索了一段时间的答案,不是这样,我以前看过了。可能需要注册,这告诉我,您在浏览器中从站点获得的流取决于身份验证。现在您没有在Java代码中实现这一点。也没有您有网站可能使用的cookie的实现。每个地方都不同。讨论“如何使用Java登录网站”。祝你好运!谢谢,我会检查一下,但我不确定这是不是解决方案,因为当我使用Jsoup时,它给了我一个错误,在我登录之前它无法访问链接。但这可能是不同的。我会研究它,谢谢。这段代码暗示了动态HTML如何工作的一个非常简单的视图。我强烈建议你使用Firebug(Firefox)或其他浏览器的同等调试工具,并研究访问页面时实际发生的情况。一般来说(20世纪90年代静态HTML网站除外)你不能在一次简单的获取中获取一个页面。它要复杂得多。可能的重复正如我所说的,我已经搜索了一段时间的答案,不是这样,我以前看过它。可能需要注册,这告诉我你从浏览器中的站点获取的流取决于身份验证。现在你没有在y中实现这一点我们的Java代码。你也没有网站可能使用的cookie的实现。到处都不同。讨论“如何使用Java登录网站”。祝你好运!谢谢,我会检查一下,但我不确定这是解决方案,因为我给我们提供了帮助