簡體   English   中英

Java-如何使用Jsoup提取Google新聞標題和鏈接?

[英]Java - How do I extract Google News Titles and Links using Jsoup?

我對使用jsoup和html很陌生。 我想知道如何從Google新聞首頁上的故事中提取標題和鏈接(如果可能)。 這是我的代碼:

    org.jsoup.nodes.Document doc = null;
                try {
                    doc = (org.jsoup.nodes.Document) Jsoup.connect("https://news.google.com/").get();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                Elements titles = doc.select("titletext");

                System.out.println("Titles: " + titles.text());


                //non existent
                for (org.jsoup.nodes.Element e: titles) {
                    System.out.println("Title: " + e.text());
                    System.out.println("Link: " + e.attr("href"));
                }

由於某種原因,我認為我的程序無法找到titletext ,因為這是代碼運行時的輸出: Titles:

非常感謝您的幫助,謝謝。

首先獲取所有以h2 html標記開頭的節點/元素

Elements elem = html.select("h2");

現在您有了一個具有一些子元素的元素(id,href,originalhref等)。 在這里您需要檢索這些所需的數據

 for(Element e: elem){
         System.out.println(e.select("[class=titletext]").text());
         System.out.println(e.select("a").attr("href"));
     }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM