繁体   English   中英

如何使用 Jsoup 从网页中获取特定 div 内的所有链接?

[英]How to get all the links inside a specific div from a webpage using Jsoup?

我正在尝试从我的项目的网页中获取li标签内 div 中的所有链接。 我得到的只是来自 first li的第一个链接。 我在下面的代码中有我的 for 循环。 任何帮助表示赞赏。

 Document document=Jsoup.connect("https://www.ndtv.com/latest?pfrom=home-topnavigation").userAgent("Mozilla/17.0").get();
 Elements links=document.select("div.nstory_header a");

 for (org.jsoup.nodes.Element link : links) {
    System.out.println("link : " + links.attr("href"));
    // System.out.println("text : " + link.text());
 }

你只是有一个错字。 这是一个工作版本:

Document document = Jsoup.connect("https://www.ndtv.com/latest?pfrom=home-topnavigation")
        .userAgent("Mozilla/17.0")
        .get();

Elements links = document.select("div.nstory_header a");
System.out.println("link : " + links.attr("href"));

for (org.jsoup.nodes.Element link : links) {
    System.out.println("text : " + link.text());
}

输出

link : https://www.ndtv.com/india-news/rahul-gandhi-mocks-pm-narendra-modi-on-rafale-he-looked-here-there-everywhere-1985608
text : "He Looked Here, There, Everywhere": Rahul Gandhi Mocks PM On Rafale
text : From Bloating To Muscle Cramps This Miracle Drink Is A Perfect Solution To Your Digestive Problems
text : Tube In Nose, Unwell Manohar Parrikar Presents Goa Budget
text : "Trump Gave Order To Kill Me": Venezuelan President Nicolas Maduro
text : "Mahatma Gandhi's Assassin Was A Fanatic, Never Forget": Swara Bhasker
text : Vaani Kapoor On Ranveer Singh Being Trolled For Pic With Cops: "It's A Norm"
text : Fortnite 7.30 Leaked Skins Listed
text : Cycling And Treadmill Workstations May Cut Stress: Foods That May Help Too
text : Fire Breaks Out At Building In Mumbai's Girgaon
text : Jobs At Delhi High Court For Graduates
text : Monday Mayawati PM, Tuesday Akhilesh Yadav: Amit Shah Jabs "Gathbandhan"
text : Manikarnika Actress Mishti Says Kangana Ranaut Made 'Fake Promises' To Cast
text : WhatsApp Hires EFF's Nate Cardozo as Privacy Policy Manager
text : Sensex Closes At 35,591, Nifty At 10,651 As Markets End Flat
text : Farah Khan's Ek Ladki Ko Dekha Toh Aisa Laga Challenge Features Her 10-Year-Old Daughter Diva

暂无
暂无

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

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