2013-04-11 23 views
1

我正在开发包含网站解析器的第一个项目。我试图了解解析器一点点,跨越了一个名为“Jsoup”库发现这里偶然发现: http://jsoup.org/download将分析后的文本添加到字符串中

然后我尝试这种代码示例,我在教程的网站上找到:

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 HTMLParserExample1 { 

    public static void main(String[] args) { 

    Document doc; 
    try { 

     // need http protocol 
     doc = Jsoup.connect("http://google.com").get(); 

     // get page title 
     String title = doc.title(); 
     System.out.println("title : " + title); 

     // get all links 
     Elements links = doc.select("a[href]"); 
     for (Element link : links) { 

      // get the value from href attribute 
      System.out.println("\nlink : " + link.attr("href")); 
      System.out.println("text : " + link.text()); 

     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    } 

} 

代码所以我决定尝试将它与我的其他应用程序(这是一个带有文本框的JFrame)结合起来

所以我试图做的是把什么放在[code] System.out中。 println(); [/ code]里面的一个字符串。 虽然这样做我是得到错误当我试图做到这一点的方式如下:

s + "\nlink : " + link.attr("href"); 
s + "text : " + link.text(); 

我得到错误,并很快意识到这wasnt做的正确的方式,让我找到了方法String.concat并决定使用它。 使用此之后它仍然没有工作,我随后也意识到,什么也应停止解析被打印出来与System.out的命令..

这里是我当前的代码:

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 JParser { 

    private String finishedParse; 

    public static void JParser() { 

     //String that should hold the finished parse 
     String finishedParse = new String(); 

     //test string used to see if what the Netbeans IDE recomended me to do work 
     String tester = new String(); 
     finishedParse = ""; 

    Document doc; 
    try { 

     //Need http protocol 
     doc = Jsoup.connect("http://google.com").get(); 

     //Get page title 
     String title = doc.title(); 
     System.out.println("title : " + title); 

     //Get all links 
     Elements links = doc.select("a[href]"); 
     for (Element link : links) { 

      //Get the value from href attribute 
      System.out.println("\nlink : " + link.attr("href")); 
      System.out.println("text : " + link.text()); 
         tester = finishedParse.concat("\nlink : " + link.attr("href")); 
         tester = finishedParse.concat("text : " + link.text()); 
         tester = finishedParse.concat("\n"); 

     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
       System.out.println(e); 
    } 
    } 

    //The method i wish to call from my other class to get the parsed text returned. 
    public String getParsedText(String parsedText){ 
     parsedText = finishedParse; 
     return parsedText; 
    } 
} 

问题现在是应该用System.out命令打印的内容没有被打印出来,而且我还没有弄清楚如何将解析后的文本放到我的字符串中。

我真的很想学习,我很难找到我的代码是错误的。我确实在网上搜索回答,但没有成功。

的错误剩下的就是以下 两个System.out的语句没有打印出任何东西到控制台,它没有工作时,我复制从教程中的代码。这两个代码在上面的帖子中,请阅读并帮助我。

的问题是,我叫在陌生的路上类,我最好的猜测是,我累了昨天,所以我的无知踢..

+0

1)你提到的错误,但你不告诉我们它们是什么。 2)只需使用+运算符进行字符串连接。 – Aurand 2013-04-11 19:27:57

+0

林将编辑帖子,并包括更明确的确切问题。 – Rakso 2013-04-11 19:28:55

回答

1

试试这个:在使用

for (Element link : links) { 

    // Get the value from href attribute 
    System.out.println("\nlink : " + link.attr("href")); 
    System.out.println("text : " + link.text()); 
    finishedParse = finishedParse.concat("\nlink : " + link.attr("href")); 
    finishedParse = finishedParse.concat("text : " + link.text()); 
    finishedParse = finishedParse.concat("\n"); 

} 

公告concat()与使用+运算符完全相同,真正的问题是您应该更新用于连接最终答案的字符串。更好的是,你应该使用StringBuilder这种工作 - 它将在原地更新(而concat()每次都会返回一个新字符串),因此效率更高。

StringBuilder sb = new StringBuilder(); 

for (Element link : links) { 

    // Get the value from href attribute 
    System.out.println("\nlink : " + link.attr("href")); 
    System.out.println("text : " + link.text()); 
    sb.append("\nlink : " + link.attr("href")); 
    sb.append("text : " + link.text()); 
    sb.append("\n"); 

} 

String finishedParse = sb.toString(); 
+0

谢谢,但我仍然有问题,上面的两行添加到finishedParse变量没有被打印出来在控制台.. – Rakso 2013-04-11 19:32:43

+0

再次感谢您花费你的时间,甚至即兴发挥你的answear,但它仍然只有50%的问题,因为应该打印出来的控制台没有打印出两个语句System.out ..不起作用。 – Rakso 2013-04-11 19:42:36

+0

这只能是因为'doc。选择(“a [href]”);'没有任何返回,但遗憾的是我对'Jsoup'不够熟悉,不知道为什么 – 2013-04-11 19:49:33

相关问题