採用jsoup 分解HTML 。使用和JS操做DOM分相似。java
示例代碼:node
import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class ParserHtml { public static void main(String[] args) throws IOException { String url = "http://www.jd.com/";//可以依據狀況選擇POST,GET請求URL Document doc = Jsoup.connect(url).data("query", "Java").userAgent("Mozilla").cookie("auth", "token").timeout(6000).get(); /* 依據ID獲取 */ Element footer = doc.getElementById("footer-2013"); System.err.println(footer.getAllElements()); /* 依據標籤獲取 */ Elements element = doc.getElementsByTag("img"); for (int i = 0; i < element.size(); i++) { System.out.println(element.get(i).attr("src")); } } }
使用JSOUP工具包cookie
版權聲明:本文博主原創文章,博客,未經贊成不得轉載。工具