java htmlunit hellod_How to extract elements from html page using HtmlUnit

I have two questions(problems actually) while parsing the html page using HtmlUnit.I tried their 'Getting Started` as well as searched google but no help.Here is my first problem.

1) I want to extract the text of following bold tag from the page

Five Dollars

2)I want to extract the entire text(including further span or link text,if present) in the last paragraph in the following structure

Hello

Haven't you registeret yet?

Registrations will close on 3 July 2012.So don't wait

Can you please one-line code snippets how can I do that?I am new to HtmlUnit.

EDIT:

HtmlUnit has getElementByName() and getElementById(), so what do we use if we want to select using class?

This will be the answer to my first question.

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

actually, I'd suggest you to use xpath and jtidy instead, like this

import java.io.IOException;

import java.net.MalformedURLException;

import java.util.List;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

import com.gargoylesoftware.htmlunit.WebClient;

import com.gargoylesoftware.htmlunit.html.HtmlForm;

import com.gargoylesoftware.htmlunit.html.HtmlItalic;

import com.gargoylesoftware.htmlunit.html.HtmlOption;

import com.gargoylesoftware.htmlunit.html.HtmlPage;

import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;

import com.gargoylesoftware.htmlunit.html.HtmlSelect;

import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

import com.gargoylesoftware.htmlunit.html.HtmlTextArea;

import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class WebScraper {

private static final String TEXT = "some random text here";

private static final String SWALLOW = "continental";

private static final String COLOR = "indigo2";

private static final String QUESTION = "why?";

private static final String NAME = "Leo";

/**

* @param args

* @throws IOException

* @throws MalformedURLException

* @throws FailingHttpStatusCodeException

*/

public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {

//to get the HTML Xpath, download and install firefox plugin Xpather from

//http://jassage.com/xpather-1.4.5b.xpi

//

//then right-click on any part of the html and choose "show in xpather"

//

//HtmlUnit is a suite for functional web app tests (headless) with a

//built-in "browser". Very useful for screen scraping.

//

//for HtmlUnit examples and usage, try

//http://htmlunit.sourceforge.net/gettingStarted.html

//

//sometimes, the HTML is malformed, so you'll need to "clean it"

//that's why I've also added JTidy to this project

WebClient webClient = new WebClient();

HtmlPage page = webClient.getPage("http://cgi-lib.berkeley.edu/ex/simple-form.html");

// System.out.println(page.asXml());

HtmlForm form = (HtmlForm) page.getByXPath("/html/body/form").get(0);

HtmlTextInput name = form.getInputByName("name");

name.setValueAttribute(NAME);

HtmlTextInput quest = form.getInputByName("quest");

quest.setValueAttribute(QUESTION);

HtmlSelect color = form.getOneHtmlElementByAttribute("select", "name", "color");

List options = color.getOptions();

for(HtmlOption op:options){

if (op.getValueAttribute().equals(COLOR)){

op.setSelected(true);

}

}

HtmlTextArea text = form.getOneHtmlElementByAttribute("textarea", "name", "text");

text.setText(TEXT);

//swallow

HtmlRadioButtonInput swallow = form.getInputByValue(SWALLOW);

swallow.click();

HtmlSubmitInput submit = form.getInputByValue("here");

//submit

HtmlPage page2 = submit.click();

// System.out.println(page2.asXml());

String color2 = ((HtmlItalic)page2.getByXPath("//dd[1]/i").get(0)).getTextContent();

String name2 = ((HtmlItalic)page2.getByXPath("//dd[2]/i").get(0)).getTextContent();

String quest2 = ((HtmlItalic)page2.getByXPath("//dd[3]/i").get(0)).getTextContent();

String swallow2 = ((HtmlItalic)page2.getByXPath("//dd[4]/i").get(0)).getTextContent();

String text2 = ((HtmlItalic)page2.getByXPath("//dd[5]/i").get(0)).getTextContent();

System.out.println(COLOR.equals(color2)

&& NAME.equals(name2)

&& QUESTION.equals(quest2)

&& SWALLOW.equals(swallow2)

&& TEXT.equals(text2));

webClient.closeAllWindows();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值