JSoup parsing data from within a tag

2019-05-30 09:07发布

I am managing to parse most of the data I need except for one as it is contained within the a href tag and I am needing the number that appears after "mmsi="

<a href="/showship.php?mmsi=235083844">Sunsail 4013</a>

my current parser fetches all the other data I need and is below. I tried a few things out the code commented out returns unspecified occasionally for an entry. Is there any way I can add to my code below so that when the data is returned the number "235083844" returns before the name "Sunsail 4013"?

try {
        File input = new File("shipMove.txt");
        Document doc = Jsoup.parse(input, null);
        Elements tables = doc.select("table.shipInfo");
        for( Element element : tables )
        {
            Elements tdTags = element.select("td");
            //Elements mmsi = element.select("a[href*=/showship.php?mmsi=]");
            // Iterate over all 'td' tags found
            for( Element td : tdTags ){
                // Print it's text if not empty
                final String text = td.text();
                if( text.isEmpty() == false )
                {
                    System.out.println(td.text());
                }
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Example of data parsed and html file here

2条回答
Fickle 薄情
2楼-- · 2019-05-30 09:52
  1. You can use attr on an Element object to retrieve a particular attribute's value
  2. Use substring to get the required value if the String pattern is consistent

Code

// Using just your anchor html tag
String html = "<a href=\"/showship.php?mmsi=235083844\">Sunsail 4013</a>";
Document doc = Jsoup.parse(html);

// Just selecting the anchor tag, for your implementation use a generic one
Element link = doc.select("a").first();

// Get the attribute value
String url = link.attr("href");

// Check for nulls here and take the substring from '=' onwards
String id = url.substring(url.indexOf('=') + 1);
System.out.println(id + " "+ link.text());

Gives,

235083844 Sunsail 4013

Modified condition in your for loop from your code:

...
    for (Element td : tdTags) {
                // Print it's text if not empty
                final String text = td.text();
                if (text.isEmpty() == false) {
                    if (td.getElementsByTag("a").first() != null) {
                        // Get the attribute value
                        String url = td.getElementsByTag("a").first().attr("href");

                        // Check for nulls here and take the substring from '=' onwards
                        String id = url.substring(url.indexOf('=') + 1);
                        System.out.println(id + " "+ td.text());
                    }
                    else {
                        System.out.println(td.text());
                    }
                }
            }
...

The above code would print the desired output.

查看更多
Root(大扎)
3楼-- · 2019-05-30 10:02

If you need value of attribute, you should use attr() method.

for( Element td : tdTags ){
    Elements aList = td.select("a");
    for(Element a : aList){
        String val = a.attr("href");
        if(StringUrils.isNotBlank(val)){
            String yourId = val.substring(val.indexOf("=") + 1);


        }
}
查看更多
登录 后发表回答