如何从Java的下拉菜单中搜寻相应的链接?

时间:2019-06-12 10:04:42

标签: javascript java web-crawler

我正在尝试用JAVA制作一个网络爬网程序,该爬网程序获得指向网站可用的所有语言的链接。大多数多语言网站都具有指向其他语言的链接,例如li > a[href],因此很容易爬网。但是如何从具有下拉菜单的站点获取链接,这些站点调用javascript函数列出项目。
例如:https://www.alphagalileo.orghttps://www.flickr.com/

编辑:获取格式为li > a[href]的链接的代码。

String REGEX = "^(EN|en|ENGLISH|English|english|...|AR|ar|Arabic)$";
//REGEX contains codes to all the available languages

Document document = Jsoup.connect(URL).get();
Elements linksOnPage = document.select("li > a[href]");

for (Element page : linksOnPage) {
    if(page.text().matches(REGEX)) {
        ArrayList<String> temporary = new ArrayList<>();
        temporary.add(URL); //URL of the parent site                  
        temporary.add(page.text()); //Language
        temporary.add(page.attr("abs:href")); //URL to the language site
        if(!langLinks.contains(temporary)) langLinks.add(temporary);
    }
}

0 个答案:

没有答案