java的asxml方法,Java HtmlPage.asXml方法代碼示例

本文整理匯總了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

​點讚 3

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;

}

}

開發者ID:knshen,項目名稱:JSearcher,代碼行數:26,

示例2: main

​點讚 3

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);

}

開發者ID:xuxueli,項目名稱:xxl-incubator,代碼行數:22,

示例3: allowAccess

​點讚 3

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;

}

開發者ID:cchabanois,項目名稱:mesfavoris,代碼行數:13,

示例4: getHtmlPageHtmlUnit

​點讚 2

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;

}

開發者ID:occidere,項目名稱:MMDownloader,代碼行數:33,

示例5: getSuapPage

​點讚 2

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();

}

}

開發者ID:marcocspc,項目名稱:SUAPPasswordResetter,代碼行數:10,

示例6: main

​點讚 2

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());

}

開發者ID:bluetata,項目名稱:crawler-jsoup-maven,代碼行數:30,

示例7: download

​點讚 2

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 inputs) {

HtmlSubmitInput button = null;

HtmlPage nextPage = null;

try {

HtmlPage page = webClient.getPage(url.getURLValue());

for(Map.Entry 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();

}

開發者ID:knshen,項目名稱:JSearcher,代碼行數:33,

示例8: getDocumentFromGateway

​點讚 2

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);

}

開發者ID:theovier,項目名稱:lernplattform-crawler,代碼行數:8,

示例9: login

​點讚 2

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);

//

// autocapitalize="off" id="loginName">

HtmlInput htmlInput = page

.getFirstByXPath("//input[@id='loginName']");

htmlInput.click();

htmlInput.setAttribute("value", userName);

//

htmlInput = page.getFirstByXPath("//input[@id='loginPassword']");

htmlInput.click();

htmlInput.setAttribute("value", passWrod);

//

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;

}

開發者ID:xiaomin0322,項目名稱:alimama,代碼行數:37,

示例10: selectEmailRecoveryAsSignInChallenge

​點讚 2

import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類

private HtmlPage selectEmailRecoveryAsSignInChallenge(WebClient webClient, HtmlPage signInChallengePage)

throws IOException {

List forms = signInChallengePage.getForms();

Optional 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;

}

開發者ID:cchabanois,項目名稱:mesfavoris,代碼行數:15,

示例11: download

​點讚 2

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 requestParameters = new ArrayList();

for(Map.Entry 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);

}

}

開發者ID:xtuhcy,項目名稱:gecco-htmlunit,代碼行數:54,

注:本文中的com.gargoylesoftware.htmlunit.html.HtmlPage.asXml方法示例整理自Github/MSDocs等源碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值