Reputation: 199
I am trying to find the circled link as seen in the image above at imgur on a webpage.Currently(as seen below) I am just pulling all a hrefs from the document and looping through it looking for the one that contains "pdf" as it is the only one on the page,is there any way to just pull a href where title = "Download offers in store" or something like that
Document doc = Jsoup.connect("http://www.dunnesstores.com/offer20/food-wine/fcp-category/home").get();
Elements links = doc.select("a[href]" + );
for (Element link : links) {
System.out.println(link.attr("href"));
if (link.attr("href").contains("pdf")){
pdfLink = link.attr("href");
}
}
Upvotes: 1
Views: 1910
Reputation: 92274
https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors
doc.select("a[title='Download offers in store']");
[attr] Represents an element with an attribute name of attr.
[attr=value] Represents an element with an attribute name of attr and whose value is exactly "value".
[attr~=value] Represents an element with an attribute name of attr whose value is a whitespace-separated list of words, one of which is exactly "value".
[attr|=value] Represents an element with an attribute name of attr. Its value can be exactly “value” or can begin with “value” immediately followed by “-” (U+002D). It can be used for language subcode matches.
[attr^=value] Represents an element with an attribute name of attr and whose first value is prefixed by "value". [attr$=value] Represents an element with an attribute name of attr and whose last value is suffixed by "value".
[attr*=value] Represents an element with an attribute name of attr and whose value contains at least one occurrence of string "value" as substring.
Upvotes: 2
Reputation: 453
You could specify a selector that matches a attribute and its value.
String pdfLink = null;
Document doc = Jsoup.connect("http://www.dunnesstores.com/offer20/food-wine/fcp-category/home").get();
Elements links = doc.select("a[title=\"Download offers in store\"]");
for (Element link : links) {
pdfLink = link.attr("abs:href");
}
System.out.println(pdfLink);
This selects every a tag where the title attribute is equals Download offers in store.
If you want to search the element by the file ending .pdf you could change the selector to:
a[href$=".pdf\"]
Upvotes: 3