Reputation: 2752
I cannot get the hyperlink from this element. I have tried element.attr("href") and element.abs("href") to no avail. Here is what I am trying to extract:
<p ><strong>Previous Chapter:</strong> <a href="http://www.mangahere.com/manga/mirai_nikki/v07/c031/">Mirai Nikki 31</a>
I want the URL from that source, but I cannot get it working.
Here is the code I have so far:
Document doc;
try{
doc = Jsoup.connect(currentURL).get();
Element e = doc.getElementsByClass("reader_tip").first().children().last().children().first();
System.out.println(e.text());
String backPage = e.attr("href");
loadPage(backPage);
}
catch(Exception ex){
ex.printStackTrace();
}
and the URL in question:
http://www.mangahere.com/manga/mirai_nikki/v07/c032/
It's probably something so simple I am overlooking it. If anyone could help it would be greatly appreciated.
Upvotes: 1
Views: 83
Reputation: 10522
You can simplify your selector to p:contains(Previous Chapter) a
, which looks for A tags that are inside P tags which contain the text "Previous Chapter".
Full example:
String url = "http://www.mangahere.com/manga/mirai_nikki/v07/c032/";
String ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.33 (KHTML, like Gecko) Chrome/27.0.1438.7 Safari/537.33";
Document doc = Jsoup.connect(url).userAgent(ua).timeout(10 * 1000).get();
Element a = doc.select("p:contains(Previous Chapter) a").first();
String backUrl = a.attr("href");
System.out.println(String.format("Back URL: %s (%s)", backUrl, a.text()));
Prints:
Back URL: http://www.mangahere.com/manga/mirai_nikki/v07/c031/ (Mirai Nikki 31)
Please see Try jsoup and the jsoup select syntax documentation.
Upvotes: 1