简体   繁体   English

如何使用 JSOUP 获取第一个链接?

[英]How to get the first link using JSOUP?

I want to use Jsoup to extract the first link on the google search results.我想使用 Jsoup 提取 google 搜索结果中的第一个链接。 For example, I search for "apple" on google.例如,我在谷歌上搜索“苹果”。 The first link I see is www.apple.com/ .我看到的第一个链接是www.apple.com/ How do I return the first link?如何返回第一个链接? I am currently able to extract all links using Jsoup:我目前能够使用 Jsoup 提取所有链接:

       new Thread(new Runnable() {
        @Override
        public void run() {
            final StringBuilder stringBuilder = new StringBuilder();
            try {
                Document doc = Jsoup.connect(sharedURL).get();
                String title = doc.title();
                Elements links = doc.select("a[href]");
                stringBuilder.append(title).append("\n");
                for (Element link : links) {
                    stringBuilder.append("\n").append(" ").append(link.text()).append(" ").append(link.attr("href")).append("\n");
                }
            } catch (IOException e) {
                stringBuilder.append("Error : ").append(e.getMessage()).append("\n");
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // set text
                    textView.setText(stringBuilder.toString());
                }
            });
        }
    }).start();

Do you mean:你的意思是:

Element firstLink = doc.select("a[href]").first();

It works for me.这个对我有用。 If you meant something else let us know.如果你的意思是别的让我们知道。 I checked the search results with the following and its a tough one to decipher as there are so many types of results that come back.. maps, news, ads, etc.我使用以下内容检查了搜索结果,很难破译,因为返回的结果类型太多了……地图、新闻、广告等。

I tidied up the code a little with the use of java lambdas:我使用 java lambdas 稍微整理了一下代码:

public static void main(String[] args) {
        new Thread(() -> {
            final StringBuilder stringBuilder = new StringBuilder();
            try {
                String sharedUrl = "https://www.google.com/search?q=apple";
                Document doc = Jsoup.connect(sharedUrl).get();
                String title = doc.title();
                Elements links = doc.select("a[href]");
                Element firstLink = links.first();  // <<<<< NEW ADDITION
                stringBuilder.append(title).append("\n");
                for (Element link : links) {
                    stringBuilder.append("\n")
                            .append(" ")
                            .append(link.text())
                            .append(" ")
                            .append(link.attr("href"))
                            .append("\n");
                }
            } catch (IOException e) {
                stringBuilder.append("Error : ").append(e.getMessage()).append("\n");
            }
            // replaced some of this for running/testing locally
            SwingUtilities.invokeLater(() -> System.out.println(stringBuilder.toString()));
        }).start();
    }

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

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