将解析的链接保存到一个字符串或一个数组java
问题描述:
String html = Jsoup.connect("www.example.com").get().html();
Scanner in = new Scanner(html);
String links ;
while(in.hasNext()){
String line = in.nextLine();
if(line.contains("sometext")){
String links += line.substring(line.indexOf("http").line.indexOf("</a>") + "\n");
}
我有上面的代码。它使用JSoup获取网页的HTML,之后,我想将它们保存为一个字符串或一个由新行分隔的数组。这是我的问题。将解析的链接保存到一个字符串或一个数组java
答
您应该继续使用jsoup来检索和解析HTML。另外,jsoup的文档address this。
String output = "";
// Get the webpage and parse it.
Document doc = Jsoup.connect(url).get();
// Get the anchors with href attribute.
// Or, you can use doc.select("a") to get all the anchors.
Elements links = doc.select("a[href]");
// Iterate over all the links and process them.
for (Element link : links) {
output += link.attr("abs:href");
}
谢谢。它工作正常....... – 2012-02-21 20:21:58