JPad
Snippets
Download
Help
Contact
JSoup Download Page Select CSS Element
Using JSoup to download a wikipedia page. Then select elements of the page taht matches certain CSS selectors.
Code
import org.jsoup.Jsoup; import org.jsoup.helper.Validate; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; Document doc = Jsoup.connect("http://en.wikipedia.org/").get(); Elements newsHeadlines = doc.select("#mp-itn b a"); Dump(newsHeadlines);
Result
Console
<a href="/wiki/Destruction_of_ivory" title="Destruction of ivory">burns the largest ever pile of ivory</a> <a href="/wiki/Equatorial_Guinean_presidential_election,_2016" title="Equatorial Guinean presidential election, 2016">is re-elected</a> <a href="/wiki/2016_Tur%C3%B8y_helicopter_crash" title="2016 Turøy helicopter crash">crashes</a> <a href="/wiki/National_Museum_of_Natural_History,_New_Delhi" title="National Museum of Natural History, New Delhi">National Museum of Natural History</a> <a href="/wiki/Hillsborough_disaster#Second_hearing" title="Hillsborough disaster">second inquest</a> <a href="/wiki/Portal:Current_events" title="Portal:Current events">Ongoing events</a> <a href="/wiki/Deaths_in_2016" title="Deaths in 2016">Recent deaths</a>
Bytecode
Compiled from "RunnContainer.java" public class io.jpad.scratch.RunnContainer { public io.jpad.scratch.RunnContainer(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String...) throws java.lang.Exception; Code: 0: ldc #2 // String http://en.wikipedia.org/ 2: invokestatic #3 // Method org/jsoup/Jsoup.connect:(Ljava/lang/String;)Lorg/jsoup/Connection; 5: invokeinterface #4, 1 // InterfaceMethod org/jsoup/Connection.get:()Lorg/jsoup/nodes/Document; 10: astore_1 11: aload_1 12: ldc #5 // String #mp-itn b a 14: invokevirtual #6 // Method org/jsoup/nodes/Document.select:(Ljava/lang/String;)Lorg/jsoup/select/Elements; 17: astore_2 18: aload_2 19: invokestatic #7 // Method io/jpad/scratch/Dumper.Dump:(Ljava/lang/Object;)Ljava/lang/Object; 22: pop 23: return }
Jpad Version:1.06
viewCount:9847
Creator:
admin
Latest Editor:
admin
Tags
jpad
jsoup
css
Download
Copy As Markdown:
**code [JSoup Download Page Select CSS Element.java](/example/1C/jsoup-download-page-select-css-element)** import org.jsoup.Jsoup; import org.jsoup.helper.Validate; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; Document doc = Jsoup.connect("http://en.wikipedia.org/").get(); Elements newsHeadlines = doc.select("#mp-itn b a"); Dump(newsHeadlines); **Result:** <a href="/wiki/Destruction_of_ivory" title="Destruction of ivory">burns the largest ever pile of ivory</a> <a href="/wiki/Equatorial_Guinean_presidential_election,_2016" title="Equatorial Guinean presidential election, 2016">is re-elected</a> <a href="/wiki/2016_Tur%C3%B8y_helicopter_crash" title="2016 Turøy helicopter crash">crashes</a> <a href="/wiki/National_Museum_of_Natural_History,_New_Delhi" title="National Museum of Natural History, New Delhi">National Museum of Natural History</a> <a href="/wiki/Hillsborough_disaster#Second_hearing" title="Hillsborough disaster">second inquest</a> <a href="/wiki/Portal:Current_events" title="Portal:Current events">Ongoing events</a> <a href="/wiki/Deaths_in_2016" title="Deaths in 2016">Recent deaths</a>