我正在开发包含网站解析器的第一个项目。我试图了解解析器一点点,跨越了一个名为“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的语句没有打印出任何东西到控制台,它没有工作时,我复制从教程中的代码。这两个代码在上面的帖子中,请阅读并帮助我。
的问题是,我叫在陌生的路上类,我最好的猜测是,我累了昨天,所以我的无知踢..
1)你提到的错误,但你不告诉我们它们是什么。 2)只需使用+运算符进行字符串连接。 – Aurand 2013-04-11 19:27:57
林将编辑帖子,并包括更明确的确切问题。 – Rakso 2013-04-11 19:28:55