简体   繁体   English

您如何使用Jsoup在网站上搜索字符串?

[英]How do you use Jsoup to search for a string on a website?

I am trying to use Jsoup to search a website to see if it contains a string. 我正在尝试使用Jsoup搜索网站以查看它是否包含字符串。 Is this even possible, and if so, how is it done? 这甚至可能吗?如果可以,怎么做?

Yes it is possible and actually quite easy if you are using Jsoup. 是的,如果您使用的是Jsoup,则可能并且实际上非常容易。 To simply see if a specific Web-Page contains a specific string then you might do something like the following example: 为了简单地查看特定网页是否包含特定字符串,您可以执行以下示例:

Let say we want to see if the following string exists within the Jsoup home web-page ( https://jsoup.org/ ): 假设我们想看看Jsoup主页网页( https://jsoup.org/ )中是否存在以下字符串:

If you have any questions on how to use jsoup

Your code could look something like this: 您的代码可能看起来像这样:

String stringToFind = "If you have any questions on how to use jsoup";
try {
    Document doc = Jsoup.connect("https://jsoup.org/").get();
    if (doc.text().contains(stringToFind)) {
        System.out.println("Yes...String exists in web-page.");
    }
    else {
        System.out.println("No...String does not exist in web-page.");
    }
}
catch (IOException ex) {
    // Do whatever you like to handle the exception...
}

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

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