简体   繁体   English

在Java中使用jsoup提取数据

[英]extracting data using jsoup in java

I am trying to run this code and i am facing the "Null Pointer Exception" in my program.I used try and catch but i donot know how to eliminate the problem. 我正在尝试运行此代码,并且我的程序中遇到“空指针异常” 。我使用了try and catch,但是我不知道如何消除此问题。 Here is the code: 这是代码:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.net.*;
import java.io.*;
import java.lang.NullPointerException;
public class WikiScraper  {

public static void main(String[] args) throws IOException
{
scrapeTopic("/wiki/Python");
}
public static void scrapeTopic(String url){
String html = getUrl("http://www.wikipedia.org/"+url);
Document doc = Jsoup.parse(html);

    String contentText = doc.select("#mw-content-text>p").first().text();
    System.out.println(contentText);
    System.out.println("The url was malformed!");
}
public static String getUrl(String url){
URL urlObj = null;
try{
urlObj = new URL(url);
}
catch(MalformedURLException e){
System.out.println("The url was malformed!");
return "";
}
URLConnection urlCon = null;
BufferedReader in = null;
String outputText = "";
try{
urlCon = urlObj.openConnection();
in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
String line = "";
while((line = in.readLine()) != null){
outputText += line;
}
in.close();
}catch(IOException e){
System.out.println("There was an error connecting to the URL");
return "";
}
return outputText;
}
}

The Error shown is: 显示的错误是:

There was an error connecting to the URL
Exception in thread "main" java.lang.NullPointerException
    at hello.WikiScraper.scrapeTopic(WikiScraper.java:17)
    at hello.WikiScraper.main(WikiScraper.java:11)

You have 你有

public static String getUrl(String url){
    // ...
    return "";
}

What always ends in an empty String. 什么总是以空字符串结尾。

Try 尝试

Document doc = Jsoup.connect("http://example.com/").get();

for example. 例如。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM