使用Jsoup解析在线字典结果



我正在尝试使用Jsoup解析在线词典结果,对此我有一个基本的了解。我发布了我试图解析的HTML。我试图抓住"棒球"one_answers"beisebol"的字符串,但我缺乏一个干净的方法来做到这一点。

这有点困难,因为这两个词在源代码中都没有唯一标识符。

但是我想这样做:

Document doc = Jsoup.connect("http://myurl.com").get();
String original = doc.select("td[width=140]").get(1).toString() //get td element which has width of 140 and get the second one
String translated = doc.select("td[align=left]").get(1).toString()//get td element which has align left and get the second one

注意:当通过抓取访问数据时,网站设计/源代码的一个小变化可能会中断您的应用程序。

这是一个使用estivate(它是一个带有与JSoup兼容的注释的Java DOM解析器)的解决方案

Document doc = Jsoup.connect("http://myurl.com").get();
EstivateMapper mapper = new EstivateMapper();
Result result = mapper.map(doc, Result.class);

的结果类定义如下:

public class Result {
    @Text(select = "td[width=140]", index=1)
    public String original;
    @Text(select = "td[align=left]", index=1)
    public String translated;
}

最新更新