多行字符串为每行分隔新字符串

发布于 2025-01-08 01:03:50 字数 828 浏览 3 评论 0原文

我有以下代码。我正在使用 jsoup 库从网站检索 URL;之后,我检查 URL 是否包含我想要的关键字,并将它们列在另一个字符串中。我的问题是我无法仅检索一个 URL。 看看我的代码:

// Get the webpage and parse it.
org.jsoup.nodes.Document doc = Jsoup.connect("http://www.examplepage").get();

// Get the anchors with href attribute.
// Or, you can use doc.select("a") to get all the anchors.
org.jsoup.select.Elements links = doc.select("a[href]");

// Iterate over all the links and process them.
for (org.jsoup.nodes.Element link : links) {
     String scrapedlinks += link.attr("abs:href")+"\n" ;
     String scrapedlinks3 ="";                  
}

 String[] links2 = links.split("\n");
 for (String newlink  : hulklinks  ) {
        if (newlink("mysearchterm")) {
            scrapedlinks3 +=newlink ;
            String[] scrapedlines = scrapedlinks3.split("\n" );
        }
 }

I have the following code. I am using the jsoup library to retrieve the URLs from a website; after that, I am checking if the URLs contain the keyword I want, and list them in another string. My problem is that I am not able to retrieve only one URL.
Have a look at my code:

// Get the webpage and parse it.
org.jsoup.nodes.Document doc = Jsoup.connect("http://www.examplepage").get();

// Get the anchors with href attribute.
// Or, you can use doc.select("a") to get all the anchors.
org.jsoup.select.Elements links = doc.select("a[href]");

// Iterate over all the links and process them.
for (org.jsoup.nodes.Element link : links) {
     String scrapedlinks += link.attr("abs:href")+"\n" ;
     String scrapedlinks3 ="";                  
}

 String[] links2 = links.split("\n");
 for (String newlink  : hulklinks  ) {
        if (newlink("mysearchterm")) {
            scrapedlinks3 +=newlink ;
            String[] scrapedlines = scrapedlinks3.split("\n" );
        }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

折戟 2025-01-15 01:03:50

我认为如果你直接将 url 存储在 Arraylist 中会更容易:

Arraylist<String> urls = new Arraylist<String>();
for (org.jsoup.nodes.Element link : links)
    urls.add(link.attr("abs:href"));

之后你可以轻松访问它们

urls.get(i);

I think it will be easier if you directly store your urls in an Arraylist:

Arraylist<String> urls = new Arraylist<String>();
for (org.jsoup.nodes.Element link : links)
    urls.add(link.attr("abs:href"));

After this you can easy access them with

urls.get(i);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文