找不到 Jsoup .text 方法
Jsoup .text method not found
我正在尝试使用 jsoup 抓取网站数据,以便在 Android Studio 项目中使用它。当我尝试使用 .text 方法获取文档的所有文本时,它显示 "cannot resolve method",即使我认为我导入了所有正确的内容。这是我的代码有问题还是其他原因?
我的代码:
Document doc = (Document) Jsoup.connect(url).get();
text = doc.text();
编辑:发现错误,org.w3c.dom.Document 被导入时正确的导入是 org.jsoup.nodes.Document
看来您创建的文档有误。以下是我们的做法:
URL url = new URL(.../*link here*/);
Document document = Jsoup.parse(url, 4000 /*timeout*/);
document.text();
您需要导入 org.jsoup.nodes.Document
.
演示:
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Main {
public static void main(String[] args) throws IOException {
Document doc = (Document) Jsoup.connect("https://www.google.com").get();
String text = doc.text();
System.out.println(text);
}
}
输出:
Google We've detected you're using an older version of Chrome.Reinstall to stay secure × About Store We've detected you're using an older version of Chrome.Reinstall to stay secure × Gmail Images Sign in Remove Report inappropriate predictions × A privacy reminder from Google Remind me later Review now United KingdomPrivacyTermsSettingsSearch settingsAdvanced searchYour data in SearchHistorySearch helpSend feedbackAdvertisingBusiness How Search works
我正在尝试使用 jsoup 抓取网站数据,以便在 Android Studio 项目中使用它。当我尝试使用 .text 方法获取文档的所有文本时,它显示 "cannot resolve method",即使我认为我导入了所有正确的内容。这是我的代码有问题还是其他原因?
我的代码:
Document doc = (Document) Jsoup.connect(url).get();
text = doc.text();
编辑:发现错误,org.w3c.dom.Document 被导入时正确的导入是 org.jsoup.nodes.Document
看来您创建的文档有误。以下是我们的做法:
URL url = new URL(.../*link here*/);
Document document = Jsoup.parse(url, 4000 /*timeout*/);
document.text();
您需要导入 org.jsoup.nodes.Document
.
演示:
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Main {
public static void main(String[] args) throws IOException {
Document doc = (Document) Jsoup.connect("https://www.google.com").get();
String text = doc.text();
System.out.println(text);
}
}
输出:
Google We've detected you're using an older version of Chrome.Reinstall to stay secure × About Store We've detected you're using an older version of Chrome.Reinstall to stay secure × Gmail Images Sign in Remove Report inappropriate predictions × A privacy reminder from Google Remind me later Review now United KingdomPrivacyTermsSettingsSearch settingsAdvanced searchYour data in SearchHistorySearch helpSend feedbackAdvertisingBusiness How Search works