Recently I've started with Jsoup and ve found this sample code.Because I'm newb I can't figure out how does this find all links from website.Could anyone explain me what happens in for loop? Mostly, I've never used this syntax of for loop before, so it's little bit confusing for me.I don't certainly understand what loop contains.Thank you!
Elements links = doc.select("a[href]");
for (Element link : links) {
// get the value from href attribute
System.out.println("\nlink : " + link.attr("href"));
System.out.println("text : " + link.text());
}
This is because Elements
implements Iterable<Element>
( org.jsoup.select.Elements
and java.lang.Iterable
)
So when you use the for
syntax you loop over your Elements
links, which is effectively a List
of type Element
. The "Element link" is the local variable assigned to each element in 'links' as you iterate.
For further information, see:
http://jsoup.org/apidocs/org/jsoup/select/Elements.html and http://jsoup.org/apidocs/index.html
As the names suggest, the classes Elements
and Element
are similar. One consist of a single element that has been selected, and the other one is a collection of multiple elements that are grouped together.
Elements links
consists of Element
-objects that have been selected.
The Elements
class implements the following interfaces in java:
Cloneable, Iterable< Element>, Collection< Element>, List< Element>.
The Elements
class is implemented using ArrayList<Element>
, thus it is easy to add and remove Element
objects from the Elements
collection.
When it comes to the for
-loop it is a simple way to iterate over each Element
object in the Elements
collection called links.
The loop will iterate through the collection, and assign the variable link
to the current Element
object in the collection called links
. Inside the for
-loop the content of the current link
will be printed, and the loop starts over with the next Element
object in the collection.
The syntax of this loop is often called a for-each
loop, since it iterates over each object in a list or collection.
Read up on it here !
Look through the Jsoup API docs to learn more about how to use it!
If you want to learn more about how Jsoup is implemented, take a look at the source code !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.