Total Pageviews

Showing posts with label jsoup. Show all posts
Showing posts with label jsoup. Show all posts

2018/03/13

[jsoup] How to use jsoup selector to retrieve value?

Problem
I try to get "2412 中華電" from this page: https://goodinfo.tw/StockInfo/StockDetail.asp?STOCK_ID=2412


jsoup provide select method to find elements that match the Selector CSS query, with this element as the starting context. Matched elements may include this element, or any of its children. But how to define the CSS selector?


1
2
3
4
5
6
    public static void main(String[] args) throws IOException {
        String cssSelector = "???";
        String url = "https://goodinfo.tw/StockInfo/StockDetail.asp?STOCK_ID=2412";
        Document doc = Jsoup.connect(url).get();
        String name = doc.select(cssSelector).text();
    }


How-To
You can use Google Chrome to help you get the CSS Selector:

Here has the sample code:
1
2
3
4
5
6
    public static void main(String[] args) throws IOException {
        String cssSelector = "body > table:nth-child(3) > tbody > tr > td:nth-child(3) > table > tbody > tr:nth-child(1) > td > table:nth-child(1) > tbody > tr > td > table > tbody > tr:nth-child(1) > td > span:nth-child(1) > a";
        String url = "https://goodinfo.tw/StockInfo/StockDetail.asp?STOCK_ID=2412";
        Document doc = Jsoup.connect(url).get();
        String name = doc.select(cssSelector).text();
    }




2017/03/07

[Redmine] How to get mail notification value in user page

Problem
I am trying to get admin value from the redmine user object. 
But I cannot get admin value, no matter using Java API or Rest API.
It can only retrieve very limited information:



How-To
We can make good use of jsoup to parse html page and get value from the specific html element.

Firstly, checking the drop down list element in html. 


Secondly, using jsoup to parse HTML
1
2
3
4
5
6
7
8
9
    String url = "http://192.168.0.1/users/" + userId + "/edit";
    Document document = Jsoup.connect(url).get();
    Elements mailNotificationElements = document.select("[name=user[mail_notification]]").select("option");
    for (Element element : mailNotificationElements) {
        if ("selected".equals(element.attr("selected"))) {
            log.debug("the mail_notification value = " + element.val());
            break;
        }
    }


Reference
[1] https://jsoup.org/



2017/03/06

[Redmine] How to get admin value in user page

Problem
I am trying to get admin value from the redmine user object. 
But I cannot get admin value, no matter using Java API or Rest API.
It can only retrieve very limited information:



How-To
We can make good use of jsoup to parse html page and get value from the specific html element.

Firstly, checking the checkbox element in html. 
We can find out the element name and the checked checkbox will add checked="checked" attribute.

If the checkbox does not be checked, it will not have checked="checked" attribute.


Secondly, using jsoup to parse HTML
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    String url = "http://192.168.0.1/users/" + userId + "/edit";
    // Connect to redmine url and get HTML document
    Document document = Jsoup.connect(url).get();
    
    // Find elements that match [name=user[admin]]
    Elements adminElements = document.select("[name=user[admin]]");
    for (Element element : adminElements) {
       // if the element has checked="checked" attribute, it means this user is administrator, 
       // or this user is a normal user
       if ("checked".equals(element.attr("checked"))) {
           log.debug("this user is admin");
           break;
       }
    }


Reference
[1] https://jsoup.org/