jsoup教程

html-css022

jsoup教程,第1张

Jsoup解析是按照字符串解析的,比如:

Document doc=Jsoup.parse(response1Str)

这句传入的response1Str就是一个String类型。因此你只需把本地html文件作为文本全读入为一个字符串,然后再用JSoup进一步解析就行了。

有关读入文件,变成字符串,我刚刚答过一个问题,你可参考一下:

http://zhidao.baidu.com/question/456256407982905445

剩下的代码,就是JSoup用类似CSS选择器的语法,取出你需要的元素,作进一步处理了。

例如:

Elements e2=doc.getElementsByTag("input")

for(Element e: e2) {

    if(e.attr("name").equals("formhash")) {

        formhashStr=e.attr("value")

        break

    }

}

System.out.println("formhash="+formhash)

上面这个代码片段是取出具有name属性为formhash的<input>标签,并打印此属性的value值。

建议去JSoup官网了解更详细的API及功能。