Java Code Examples for org.jsoup.select.Elements#last()

The following examples show how to use org.jsoup.select.Elements#last() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private Repository parseCollectionsRepositoryData(Element element) throws Exception{
        String fullName = element.select("div > h1 > a").attr("href");
        fullName = fullName.substring(1);
        String owner = fullName.substring(0, fullName.lastIndexOf("/"));
        String repoName = fullName.substring(fullName.lastIndexOf("/") + 1);
//        String ownerAvatar = element.select("div > div > a > img").attr("src");
        String ownerAvatar = "";

        Elements articleElements = element.getElementsByTag("div");
        Element descElement = articleElements.get(articleElements.size() - 2);
        StringBuilder desc = new StringBuilder("");
        for(TextNode textNode : descElement.textNodes()){
            desc.append(textNode.getWholeText());
        }

        Element numElement = articleElements.last();
        String starNumStr =  numElement.select("a").get(0).textNodes().get(1).toString();
        String forkNumStr =  numElement.select("a").get(1).textNodes().get(1).toString();
        String language = "";
        Elements languageElements = numElement.select("span > span > span");
        if(languageElements.size() > 0){
            language = numElement.select("span > span > span").get(1).textNodes().get(0).toString();
        }

        Repository repo = new Repository();
        repo.setFullName(fullName);
        repo.setName(repoName);
        User user = new User();
        user.setLogin(owner);
        user.setAvatarUrl(ownerAvatar);
        repo.setOwner(user);

        repo.setDescription(desc.toString());
        repo.setStargazersCount(Integer.parseInt(starNumStr.replaceAll(" ", "")));
        repo.setForksCount(Integer.parseInt(forkNumStr.replaceAll(" ", "")));
        repo.setLanguage(language);

        return repo;
    }
 
Example 2
Source Project: SteamGifts   File: LoadDiscussionDetailsTask.java    License: MIT License 5 votes vote down vote up
@NonNull
private DiscussionExtras loadExtras(Document document) {
    DiscussionExtras extras = new DiscussionExtras();

    // Load the description
    Element description = document.select(".comment__display-state .markdown").first();
    if (description != null) {
        // This will be null if no description is given.
        description.select("blockquote").tagName("custom_quote");
        extras.setDescription(Utils.loadAttachedImages(extras, description));
    }

    // Can we send a comment?
    Element xsrf = document.select(".comment--submit form input[name=xsrf_token]").first();
    if (xsrf != null)
        extras.setXsrfToken(xsrf.attr("value"));


    // Load comments
    Elements commentsNode = document.select(".comments");
    if (commentsNode.size() > 1) {
        Element rootCommentNode = commentsNode.last();
        if (rootCommentNode != null)
            Utils.loadComments(rootCommentNode, extras, 0, fragment.getAdapter().isViewInReverse(), false, Comment.Type.COMMENT);
    }

    // Do we have a poll?
    Element pollElement = document.select(".poll").first();
    if (pollElement != null) {
        try {
            extras.setPoll(loadPoll(pollElement));
        } catch (Exception e) {
            Log.w(TAG, "unable to load poll", e);
        }
    }

    return extras;
}
 
Example 3
Source Project: tutorials   File: JsoupParserIntegrationTest.java    License: MIT License 5 votes vote down vote up
@Test
public void examplesTraversing() {
    Elements sections = doc.select("section");

    Element firstSection = sections.first();
    Element lastSection = sections.last();
    Element secondSection = sections.get(2);
    Elements allParents = firstSection.parents();
    Element parent = firstSection.parent();
    Elements children = firstSection.children();
    Elements siblings = firstSection.siblingElements();

    sections.forEach(el -> System.out.println("section: " + el));
}
 
Example 4
@Override
public void run() {
    List<Reply> replies = new ArrayList<>();

    boolean succeed = false;
    boolean hasMore = false;

    try {
        Document doc = get(mUrl);

        Elements elements = doc.select("div.reply-item");

        for (Element element : elements) {
            Reply reply = new Reply();

            Elements topicElements = element.select("span.title a");
            if (topicElements.isEmpty()) {
                continue;
            }

            Topic topic = new Topic();
            topic.setTitle(topicElements.first().text());
            topic.setUrl(topicElements.first().absUrl("href"));

            reply.setTopic(topic);

            Elements contentElements = element.select("div.content");
            if (contentElements.isEmpty()) {
                continue;
            }

            reply.setContent(contentElements.outerHtml());

            replies.add(reply);
        }

        succeed = true;

        Elements paginationElements = doc.select("ul.pagination");
        if (!paginationElements.isEmpty()) {
            Elements disabledElements = paginationElements.select("li.disabled");
            if (disabledElements.isEmpty()) {
                hasMore = true;
            } else if (disabledElements.last() != null) {
                Elements disableLinkElements = disabledElements.last().select("a");
                if (!ConstantUtil.NEXT_PAGE.equals(disableLinkElements.text())) {
                    hasMore = true;
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (succeed) {
        ListResult<Reply> replyList = new ListResult<>();
        replyList.setData(replies);
        replyList.setHasMore(hasMore);
        successOnUI(replyList);
    } else {
        failedOnUI("获取回复列表失败");
    }
}
 
Example 5
@Override
public void run() {
    List<Notification> notifications = new ArrayList<>();

    boolean succeed = false;
    boolean hasMore = false;
    try {
        Document doc = get(mUrl);

        Elements elements = doc.select("div.notification-item");

        for (Element element : elements) {
            Notification notification = createNotificationFromElement(element);
            notifications.add(notification);
        }

        succeed = true;

        Elements paginationElements = doc.select("ul.pagination");
        if (!paginationElements.isEmpty()) {
            Elements disabledElements = paginationElements.select("li.disabled");
            if (disabledElements.isEmpty()) {
                hasMore = true;
            } else if (disabledElements.last() != null) {
                Elements disableLinkElements = disabledElements.last().select("a");
                if (!ConstantUtil.NEXT_PAGE.equals(disableLinkElements.text())) {
                    hasMore = true;
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (succeed) {
        ListResult<Notification> notificationList = new ListResult<>();
        notificationList.setData(notifications);
        notificationList.setHasMore(hasMore);
        successOnUI(notificationList);
    } else {
        failedOnUI("获取消息提醒列表失败");
    }
}
 
Example 6
@Override
public void run() {
    List<Topic> topics = new ArrayList<>();

    boolean succeed = false;
    boolean hasMore = false;
    try {
        Document doc = get(mUrl);

        tryFixAuthStatus(doc);

        Elements elements = doc.select("div.topic-item");

        for (Element element : elements) {
            Topic topic = createTopicFromElement(element);
            topics.add(topic);
        }

        succeed = true;

        Elements paginationElements = doc.select("ul.pagination");
        if (!paginationElements.isEmpty()) {
            Elements disabledElements = paginationElements.select("li.disabled");
            if (disabledElements.isEmpty()) {
                hasMore = true;
            } else if (disabledElements.last() != null) {
                Elements disableLinkElements = disabledElements.last().select("a");
                if (!ConstantUtil.NEXT_PAGE.equals(disableLinkElements.text())) {
                    hasMore = true;
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (succeed) {
        ListResult<Topic> topicList = new ListResult<>();
        topicList.setData(topics);
        topicList.setHasMore(hasMore);
        successOnUI(topicList);
    } else {
        failedOnUI("获取主题列表失败");
    }
}
 
Example 7
private Set<Book> queryBooks(Elements booksliNode) {
    Set<Book> books = new HashSet<>();
    for (Element element : booksliNode) {
        //获取书名和id
        String name = null, id = null, author = null, publishDate = null, theme = null, detailBookClass = null;
        BookClass bookBookClass;
        Elements nameIdNode = element.select("p[class=name]");
        if (nameIdNode != null) {
            name = nameIdNode.text();
            Elements idNode = nameIdNode.select("a[onclick]");
            if (idNode != null && idNode.size() > 0) {
                String idOnClick = idNode.get(0).attr("onclick");
                int start = idOnClick.indexOf("(") + 1, end = idOnClick.lastIndexOf(",");
                if (start != 0 && end != -1) {
                    id = idOnClick.substring(start, end);
                }
            }
        }
        //获取分类
        BookClass[] bookClasses = new BookClass[0];
        Elements infoNode = element.select("p[class=info]");
        if (infoNode != null) {
            Elements bookInfos = infoNode.select("a");
            if (bookInfos != null && bookInfos.size() > 0) {
                Element terminalCataNode = bookInfos.last();
                bookInfos.remove(terminalCataNode);
                List<BookClass> tmplist = bookInfos.stream()
                        .map(bookInfo -> getBookCata(bookInfo, false))
                        .filter(Objects::nonNull)
                        .collect(Collectors.toList());
                BookClass terminalBookClass = getBookCata(terminalCataNode, true);
                if (terminalBookClass != null) {
                    tmplist.add(terminalBookClass);
                }
                bookClasses = tmplist.toArray(bookClasses);
            }
        }
        bookBookClass = this.link(bookClasses);

        //获取作者,出版日期,主题词,分类
        String info = element.text();
        Pattern pattern = Pattern.compile("\\d+\\. (.*) 作者[::](.*) 出版日期[::](\\d+).*?(?:主题词[::](.+))? 分类[::](.*)");
        Matcher matcher = pattern.matcher(info);
        while (matcher.find()) {
            name = matcher.group(1);
            author = matcher.group(2);
            publishDate = matcher.group(3);
            theme = matcher.group(4);
            detailBookClass = matcher.group(5);
        }
        Pattern minPattern = Pattern.compile(".*(《.*》).*");
        Matcher minMatcher = minPattern.matcher(info);
        while (minMatcher.find()) {
            name = minMatcher.group(1);
        }

        //汇总书本
        if (name != null && id != null) {
            Book book = new Book(id, name, author, publishDate, theme, bookBookClass, detailBookClass);
            book.setCookie(cookie);
            books.add(book);
            if (bookBookClass.isTerminal()) {
                ((TerminalBookClass) bookBookClass).addBook(book);
            } else {
                System.out.println("未获取到分类信息,将不被归档 " + book);
            }
        } else {
            System.out.println("error: " + info);
        }
    }
    return books;
}
 
Example 8
private Set<Book> queryBooks(Elements booksliNode) {
    Set<Book> books = new HashSet<>();
    for (Element element : booksliNode) {
        //获取书名和id
        String name = null, id = null, author = null, publishDate = null, theme = null, detailBookClass = null;
        BookClass bookBookClass;
        Elements nameIdNode = element.select("p[class=name]");
        if (nameIdNode != null) {
            name = nameIdNode.text();
            Elements idNode = nameIdNode.select("a[onclick]");
            if (idNode != null && idNode.size() > 0) {
                String idOnClick = idNode.get(0).attr("onclick");
                int start = idOnClick.indexOf("(") + 1, end = idOnClick.lastIndexOf(",");
                if (start != 0 && end != -1) {
                    id = idOnClick.substring(start, end);
                }
            }
        }
        //获取分类
        BookClass[] bookClasses = new BookClass[0];
        Elements infoNode = element.select("p[class=info]");
        if (infoNode != null) {
            Elements bookInfos = infoNode.select("a");
            if (bookInfos != null && bookInfos.size() > 0) {
                Element terminalCataNode = bookInfos.last();
                bookInfos.remove(terminalCataNode);
                List<BookClass> tmplist = bookInfos.stream()
                        .map(bookInfo -> getBookCata(bookInfo, false))
                        .filter(Objects::nonNull)
                        .collect(Collectors.toList());
                BookClass terminalBookClass = getBookCata(terminalCataNode, true);
                if (terminalBookClass != null) {
                    tmplist.add(terminalBookClass);
                }
                bookClasses = tmplist.toArray(bookClasses);
            }
        }
        bookBookClass = new RootBookClass().link(bookClasses);

        //获取作者,出版日期,主题词,分类
        String info = element.text();
        Pattern pattern = Pattern.compile("\\d+\\. (.*) 作者[::](.*) 出版日期[::](\\d+).*?(?:主题词[::](.+))? 分类[::](.*)");
        Matcher matcher = pattern.matcher(info);
        while (matcher.find()) {
            name = matcher.group(1);
            author = matcher.group(2);
            publishDate = matcher.group(3);
            theme = matcher.group(4);
            detailBookClass = matcher.group(5);
        }
        Pattern minPattern = Pattern.compile(".*(《.*》).*");
        Matcher minMatcher = minPattern.matcher(info);
        while (minMatcher.find()) {
            name = minMatcher.group(1);
        }

        //汇总书本
        if (name != null && id != null) {
            Book book = new Book(id, name, author, publishDate, theme, bookBookClass, detailBookClass);
            book.setCookie(cookie);
            books.add(book);
            if (bookBookClass.isTerminal()) {
                ((TerminalBookClass) bookBookClass).addBook(book);
            } else {
                System.out.println("未获取到分类信息,将不被归档 " + book);
            }
        } else {
            System.out.println("error: " + info);
        }
    }
    return books;
}