本文整理匯總了Java中com.gargoylesoftware.htmlunit.html.HtmlPage.asXml方法的典型用法代碼示例。如果您正苦於以下問題:Java HtmlPage.asXml方法的具體用法?Java HtmlPage.asXml怎麽用?Java HtmlPage.asXml使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.gargoylesoftware.htmlunit.html.HtmlPage
的用法示例。
在下文中一共展示了HtmlPage.asXml方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: mockLogin
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
public String mockLogin(URL url, String username, String password) {
try {
HtmlPage page1 = webClient.getPage(url.getURLValue());
// find the login form
HtmlForm form = page1.getForms().get(0);
// fill in the input
HtmlInput hi = form.getInputByName("commit");
HtmlTextInput textField = form.getInputByName("login");
HtmlPasswordInput pass = form.getInputByName("password");
textField.click();
textField.setValueAttribute(username);
pass.click();
pass.setValueAttribute(password);
// push the button
HtmlPage page2 = hi.click();
return page2.asXml();
} catch(IOException ioe) {
ioe.printStackTrace();
return null;
}
}
示例2: main
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
// 瀏覽器
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setUseInsecureSSL(true);//支持https
webClient.getOptions().setJavaScriptEnabled(true); // 啟用JS解釋器,默認為true
webClient.getOptions().setCssEnabled(false); // 禁用css支持
webClient.getOptions().setThrowExceptionOnScriptError(false); // js運行錯誤時,是否拋出異常
webClient.getOptions().setTimeout(10000); // 設置連接超時時間 ,這裏是10S。如果為0,則無限期等待
webClient.getOptions().setDoNotTrackEnabled(false);
webClient.setJavaScriptTimeout(8000);//設置js運行超時時間
webClient.waitForBackgroundJavaScript(500);//設置頁麵等待js響應時間,
// proxy
//webClient.getOptions().setProxyConfig(new ProxyConfig("IP", 80));
HtmlPage page = webClient.getPage("http://---");
String pageXml = page.asXml(); //以xml的形式獲取響應文本
System.out.println(pageXml);
}
示例3: allowAccess
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
private HtmlPage allowAccess(WebClient webClient, HtmlPage allowAccessPage) throws IOException {
HtmlButton allowAccessButton = (HtmlButton) allowAccessPage.getElementById("submit_approve_access");
if (allowAccessButton == null) {
throw new RuntimeException("Cannot find allow access button in html page :\n" + allowAccessPage.asXml());
}
webClient.waitForBackgroundJavaScriptStartingBefore(WAIT_DELAY_MS);
// allowAccessButton.click() does not work because
// allowAccessButton.isVisible() is false
// for some reason (click() was working with htmlunit 2.23)
HtmlPage tokenPage = clickButtonIgnoringVisibility(allowAccessButton);
return tokenPage;
}
示例4: getHtmlPageHtmlUnit
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
/**
* HtmlUnit을 이용한 HTML 코드 파싱.
* @param eachArchiveAddress 실제 만화가 담긴 아카이브 주소
* @return 성공 시 html 코드를 리턴
*/
private String getHtmlPageHtmlUnit(String eachArchiveAddress) throws Exception {
/* 필수! 로그 메세지 출력 안함 -> HtmlUnit 이용시 Verbose한 로그들이 너무 많아서 다 끔 */
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
System.out.print("일반 연결 시도중 ... ");
WebClient webClient = new WebClient();
webClient.getOptions().setRedirectEnabled(true);
WebRequest req = new WebRequest(new URL(eachArchiveAddress));
req.setHttpMethod(HttpMethod.POST);
req.setAdditionalHeader("User-Agent", UserAgent.getUserAgent());
req.setAdditionalHeader("Accept-Encoding", "gzip"); //20171126 gzip 추가
req.getRequestParameters().add(new NameValuePair("pass", PASSWORD)); //비밀번호 post 방식 전송
HtmlPage page = webClient.getPage(req);
//Html코드를 포함한 페이지 소스코드가 담길 스트링
String pageSource = page.asXml();
/** 여기도 페이지 파싱 실패 시 검증하는 코드 들어가야 됨 **/
webClient.close();
System.out.println("성공");
return pageSource;
}
示例5: getSuapPage
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
public String getSuapPage(String url) throws IOException {
HtmlPage pagina = conn.getPage(url);
if (pagina.getTitleText().equals(SuapConnection.SUAP_LOGIN_PAGE_TITLE)) {
return login(url).asXml();
} else {
return pagina.asXml();
}
}
示例6: main
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
// 屏蔽HtmlUnit等係統 log
LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log","org.apache.commons.logging.impl.NoOpLog");
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
java.util.logging.Logger.getLogger("org.apache.http.client").setLevel(Level.OFF);
String url = "https://www.douyin.com/share/video/6496703951436516621/?mid=6484356820260686606";
System.out.println("Loading page now-----------------------------------------------: "+url);
/* HtmlUnit 模擬瀏覽器 */
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setJavaScriptEnabled(true); // 啟用JS解釋器,默認為true
webClient.getOptions().setCssEnabled(false); // 禁用css支持
webClient.getOptions().setThrowExceptionOnScriptError(false); // js運行錯誤時,是否拋出異常
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setTimeout(10 * 1000); // 設置連接超時時間
HtmlPage page = webClient.getPage(url);
webClient.waitForBackgroundJavaScript(30 * 1000); // 等待js後台執行30秒
String pageAsXml = page.asXml();
/* Jsoup解析處理 */
// Document doc = Jsoup.parse(pageAsXml, "https://bluetata.com/");
Document doc = Jsoup.parse(pageAsXml);
Elements pngs = doc.select("img[src$=.png]"); // 獲取所有圖片元素集
// 其他操作
System.out.println(doc.toString());
}
示例7: download
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
/**
* return the html of url
* @param url : form data
* @param inputs : input name -> input value
* @return
*/
public String download(URL url, Map<String, String> inputs) {
HtmlSubmitInput button = null;
HtmlPage nextPage = null;
try {
HtmlPage page = webClient.getPage(url.getURLValue());
for(Map.Entry<String, String> input : inputs.entrySet()) {
String form_name = input.getKey().split("\\.")[0];
String input_name = input.getKey().split("\\.")[1];
HtmlForm form = page.getFormByName(form_name);
if(input_name.equals("button")) {
button = form.getInputByValue(input.getValue());
}
else {
HtmlTextInput text_input = form.getInputByName(input_name);
text_input.setValueAttribute(input.getValue());
}
}
nextPage = button.click();
} catch(IOException ioe) {
ioe.printStackTrace();
}
return nextPage.asXml();
}
示例8: getDocumentFromGateway
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
public static DownloadableDocument getDocumentFromGateway(HtmlPage gatewayPage, String courseName, Term term) {
String pageContent = gatewayPage.asXml();
String name = fetchFileName(gatewayPage);
String downloadLink = fetchDownloadLink(pageContent);
String extension = fetchFileExtension(downloadLink);
return new DownloadableDocument(name, downloadLink, courseName, extension, term);
}
示例9: login
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
public static boolean login(WebClient client, String userName,
String passWrod) throws Exception {
boolean flag = false;
try {
String s = "https://passport.sina.cn/signin/signin?entry=wapsso&vt=4&r=http%3A%2F%2Fmy.sina.cn%2F%3Fpos%3D108%26vt%3D4%26m%3D78fc51068140045a973a3aeab4db2381&revalid=1";
HtmlPage page = client.getPage(s);
// <input type="text" placeholder="微博帳號/手機號/郵箱" autocorrect="off"
// autocapitalize="off" id="loginName">
HtmlInput htmlInput = page
.getFirstByXPath("//input[@id='loginName']");
htmlInput.click();
htmlInput.setAttribute("value", userName);
// <input type="password" placeholder="密碼" id="loginPassword">
htmlInput = page.getFirstByXPath("//input[@id='loginPassword']");
htmlInput.click();
htmlInput.setAttribute("value", passWrod);
// <a id="loginAction" class="btn_login" href="javascript:;">登錄</a>
HtmlElement a = page.getFirstByXPath("//a[@id='loginAction']");
page = a.click();
client.setJavaScriptTimeout(5000);
String str = page.asXml();
if (!str.contains("QQ帳號登錄")) {
flag = true;
logger.error("登錄成功");
}
} catch (Exception e) {
logger.error(e);
flag = false;
}
return flag;
}
示例10: selectEmailRecoveryAsSignInChallenge
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
private HtmlPage selectEmailRecoveryAsSignInChallenge(WebClient webClient, HtmlPage signInChallengePage)
throws IOException {
List<HtmlForm> forms = signInChallengePage.getForms();
Optional<HtmlForm> kpeForm = forms.stream()
.filter(form -> "/signin/challenge/kpe/2".equals(form.getActionAttribute())).findFirst();
if (!kpeForm.isPresent()) {
throw new RuntimeException(
"Cannot find recovery by email form in html page :\n" + signInChallengePage.asXml());
}
HtmlButton button = (HtmlButton) kpeForm.get().getElementsByTagName("button").get(0);
HtmlPage htmlPage = button.click();
webClient.waitForBackgroundJavaScriptStartingBefore(WAIT_DELAY_MS);
return htmlPage;
}
示例11: download
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
public HttpResponse download(HttpRequest request, int timeout) throws DownloadException {
try {
URL url = new URL(request.getUrl());
WebRequest webRequest = new WebRequest(url);
webRequest.setHttpMethod(HttpMethod.GET);
if(request instanceof HttpPostRequest) {//post
HttpPostRequest post = (HttpPostRequest)request;
webRequest.setHttpMethod(HttpMethod.POST);
List<NameValuePair> requestParameters = new ArrayList<NameValuePair>();
for(Map.Entry<String, Object> entry : post.getFields().entrySet()) {
NameValuePair nvp = new NameValuePair(entry.getKey(), entry.getValue().toString());
requestParameters.add(nvp);
}
webRequest.setRequestParameters(requestParameters);
}
//header
boolean isMobile = SpiderThreadLocal.get().getEngine().isMobile();
webRequest.setAdditionalHeader("User-Agent", UserAgent.getUserAgent(isMobile));
webRequest.setAdditionalHeaders(request.getHeaders());
//proxy
HttpHost proxy = Proxys.getProxy();
if(proxy != null) {
webRequest.setProxyHost(proxy.getHostName());
webRequest.setProxyPort(proxy.getPort());
}
//timeout
this.webClient.getOptions().setTimeout(timeout);
//request,response
webClient.getPage(webRequest);
HtmlPage page = webClient.getPage(request.getUrl());
HttpResponse resp = new HttpResponse();
WebResponse webResponse = page.getWebResponse();
int status = webResponse.getStatusCode();
resp.setStatus(status);
if(status == 302 || status == 301) {
String redirectUrl = webResponse.getResponseHeaderValue("Location");
resp.setContent(UrlUtils.relative2Absolute(request.getUrl(), redirectUrl));
} else if(status == 200) {
String content = page.asXml();
resp.setContent(content);
resp.setRaw(webResponse.getContentAsStream());
String contentType = webResponse.getContentType();
resp.setContentType(contentType);
String charset = getCharset(request.getCharset(), contentType);
resp.setCharset(charset);
} else {
throw new DownloadException("ERROR : " + status);
}
return resp;
} catch(Exception ex) {
throw new DownloadException(ex);
}
}