【问题标题】:How to extract elements from html page using HtmlUnit如何使用 HtmlUnit 从 html 页面中提取元素
【发布时间】:2014-02-12 06:18:10
【问题描述】:

我在使用HtmlUnit解析html页面时有两个问题(实际上是问题)。我尝试了他们的“入门”以及搜索谷歌但没有帮助。这是我的第一个问题。

1)我想从页面中提取下面bold标签的文字

<b class="productPrice">Five Dollars</b>

2)我想提取以下结构中最后一段中的整个文本(包括进一步的跨度或链接文本,如果存在)

<div class="alertContainer">
<p>Hello</p>
<p>Haven't you registeret yet?</p>
<p>Registrations will close on 3 July 2012.<span>So don't wait</span></p>
</div>

请问单行代码sn-ps我该怎么做?我是HtmlUnit的新手。

编辑:

HtmlUnit有getElementByName()getElementById(),那么如果我们想用class来选择呢?

这将是我第一个问题的答案。

【问题讨论】:

  • 你试过getElementsByAttribute()getOneHtmlElementByAttribute()吗? (其中属性名称是“类”)

标签: java web-scraping htmlunit


【解决方案1】:

其实,我建议你改用 xpath 和 jtidy,像这样

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

    }

}

【讨论】:

  • :我们可以在 HtmlUnit 中使用正则表达式吗?就像问题是 &lt;div class='fresh_article_832'&gt; 现在 832 可能会改变,所以我可以在我的 xpath 中使用类似 fresh_article_[0-9]* 的东西吗?或者有什么替代方案。
  • 我不知道 httpunit xpath 是否支持 fn:matches() 但你可以试一试:-)
  • :XPather 太棒了。+1
  • LeonardoKenji:再次感谢 xpather。对于我在评论中提出的问题。它也有效。实际上,诀窍是省略 id 并使用父容器访问元素(文本)(如div、p、b 等)。已接受答案
  • :我遇到了问题。html 格式错误,所以我得到了错误的结果。你提到了 JTidy。我该如何使用它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-26
  • 1970-01-01
相关资源
最近更新 更多