Jsoup url, get url by link name

I wanna get url by the link name.

<a href="download.asp?softid=1&amp;downid=2&id=65367" target="_blank">download</a>

<a href="www.baidu.com" target="_blank">ad</a>

so what i want is the first url as the link name is download.

My question is how to get url by link name.

I know a complete solution is to get all elements and use if(a.text().contains(download) ). But I guess there is a simple way.

Thanks

Answers


Well, the best way would be to get all the < a>s, which contain hrefs, and get the hrefs attributes. Just like this:

Document doc = Jsoup.connect("whatever url").get();

Elements a = doc.select("a[href]");

String href;

for (Element elem : a) {
    href = a.attr("href");
}

Now.. Which hrefs you wanna get is enterely up to you. But I think you'd have to use the

.contains(""); 
.endsWith(""); 
.startsWith("");

Oh, and maybe you could try using the getters from the doc variable.

.getElementsByAttributeValue("a[href]", "download");


Use a pseudo-selector. For example,

Document doc = Jsoup.connect(url).get();
Elements a = doc.select("a[href]:contains(download)");

Depending on what exactly you are trying to accomplish, you might want to use containsOwn to avoid searching within child elements, or use matches/matchesOwn if you want to use a regex to get elements that contain ONLY the text "download". That regex would be

^download$

See the Selector documentation.


Need Your Help

Error when compiling a cydia tweak GPUImage

ios iphone jailbreak theos tweak

I am writing a tweak that requires GPUImage framework but I am not sure how to install it. Do I just copy all the headers to theos/include folder? (in this case i am getting "undefined symbols for

Flex 4 swc size smaller compiled by FlashBuilder than using ANT

ant adobe flex4 flash-builder

I'm trying to figure out why when I compile my Flex 4 Library swc using a simple ANT tasks, it compiles to about three times the size of the swc that's compiled by FlashBuilder.