问题
I'm looking to do some web crawling/scraping and I did some research and discovered Jsoup. The only problem I'm having is with the imports. The videos I've watched and examples I've seen have all had matching code to mine but for whatever reason their imports worked and mine don't. All four of mine give the error: The import org.jsoup cannot be resolved. Please help.
package com.stackoverflow.q2835505;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class test {
public static void main(String[] args) throws Exception {
String url = "http://stackoverflow.com/questions/2835505";
Document document = Jsoup.connect(url).get();
String question = document.select("#question .post-text").text();
System.out.println("Question: " + question);
Elements answerers = document.select("#answers .user-details a");
for (Element answerer : answerers) {
System.out.println("Answerer: " + answerer.text());
}
}
}
回答1:
jsoup is a external library and therefore you need to download the jar manually. Go to http://java2s.com/Code/Jar/j/Downloadjsoup160jar.htm or some website and get the jar file.
Once you download the jar file. Right click on your project -> Properties -> Java Build Path -> Click on Add External Jar -> (select the jar where you downloaded) -> press ok and error will go away.
回答2:
You should add them as an external jar file from https://jsoup.org/download under: right click on project > properties > build java path > libraries > add external jar file
Then you can "clean" or "restart" project from menu.
回答3:
So, the only thing I had to do that wasn't mentioned is adding a "requires" field in my module info file.
module bookstoreDB {
requires java.sql;
requires org.jsoup;
}
All the other suggestions i looked over thourougly, but nothing resolved (because my settings already matched that of the suggestions) and then it dawned on me that I have a module-info file set up for this program and I hadn't set the requires field. Hope this helps.
来源:https://stackoverflow.com/questions/32488348/jsoup-import-errors