i need java project using jsoup (if think there more efficient way achieve purpose, please let me know). purpose of program parse useful information different urls , put in text file. not expert in html or javascript, therefore, has been difficult me code in java want parse. in website see in code below 1 of examples, information interests me parse jsoup can see in table under “routing”(route, location, vessel/voyage, container arrival date, container departure date; = origin, seattle ssa terminal t18, 26 jun 15 a, 26 jun 15 a… , on). far, jsoup able parse title of website, yet have been unsuccessful in getting of body. here code used, got online source:
import org.jsoup.jsoup; import org.jsoup.nodes.document; import org.jsoup.nodes.element; import org.jsoup.select.elements; public class jsouptest71115 { public static void main(string[] args) throws exception { string url = "http://google.com/gentrack/trackingmain.do " + "?trackinput01=999061985"; document document = jsoup.connect(url).get(); string title = document.title(); system.out.println("title : " + title); string body = document.select("body").text(); system.out.println("body: " + body); } }
working code:
import org.jsoup.connection; import org.jsoup.jsoup; import org.jsoup.nodes.element; import org.jsoup.select.elements; import java.io.ioexception; import java.util.arraylist; public class sample { public static void main(string[] args) { string url = "http://homeport8.apl.com/gentrack/blroutingpopup.do"; try { connection.response response = jsoup.connect(url) .data("blnbr", "999061985") // tracking number .method(connection.method.post) .execute(); element tableelement = response.parse().getelementsbytag("table") .get(2).getelementsbytag("table") .get(2); elements trelements = tableelement.getelementsbytag("tr"); arraylist<arraylist<string>> tablearraylist = new arraylist<>(); (element trelement : trelements) { arraylist<string> columnlist = new arraylist<>(); (int = 0; < 5; i++) { columnlist.add(i, trelement.children().get(i).text()); } tablearraylist.add(columnlist); } system.out.println("origin/location: " +tablearraylist.get(1).get(1));// row , column number system.out.println("discharge port/container arrival date: " +tablearraylist.get(5).get(3)); } catch (ioexception e) { e.printstacktrace(); } } } output:
origin/location: seattle ssa terminal (t18), wa
discharge port/container arrival date: 23 jul 15 e
Comments
Post a Comment