Jsoup Import Errors

大城市里の小女人 提交于 2021-02-19 01:30:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!