[英]Getting data from a webpage with login requirement Java
因此,最近我決定自學如何從網頁獲取數據。 我設法從另一個網頁從JSON獲取數據,但是當我嘗試從該網站復制所有內容時,它沒有顯示我真正需要的數據。
我正在嘗試的頁面例如: http : //www.tremorgames.com/index.php? action=shop&page =2 (您可能需要注冊)。 我要獲取的數據例如是游戲名稱/價格或股票,如果我可以獲取一個,那么我將能夠獲取全部。
問題是開發工具會顯示代碼,但是當我嘗試使用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以獲取數據以及您可能需要的其他任何內容)。
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;
}
當您檢查網站的源代碼時,loginuser文本就是用戶名的文本字段。
HtmlInput username = currentPage.getElementByName("loginuser");
當您檢查網站的源代碼時,loginpassword文本就是密碼的文本字段。
HtmlInput password = currentPage.getElementByName("loginpassword");
user是您的用戶名(字符串類型),pass是您的密碼(字符串類型)
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后,您將擁有在開發人員工具中看到的完全相同的文本/代碼,以后您只需要在其中搜索所需的數據即可。
希望這會幫助人們並節省時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.