Как получить все ссылки внутри определенного div с веб-страницы, используя Jsoup? - PullRequest
0 голосов
/ 30 января 2019

Я пытаюсь получить все ссылки внутри div в тегах li с веб-страницы для моего проекта.Все, что я получаю, это первая ссылка только с первого 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());
 }

1 Ответ

0 голосов
/ 30 января 2019

У вас просто опечатка.Вот рабочая версия:

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
...