chandler
chandler

Reputation: 61

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

Upvotes: 0

Views: 4403

Answers (2)

jpwr
jpwr

Reputation: 244

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.

Upvotes: 2

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");

Upvotes: 2

Related Questions