So, I'm web scraping Udemy to get the all the details about the course which is on sale for free through the link where I used to get price (free or not) if free then title of the course, time remaining, rating of the course etc.
It was all working fine but suddenly it started giving me NullPointerException whenever it was trying to get the price of the course.
Element is as follow
<div class="price-text--price-part--Tu6MH udlite-clp-discount-price udlite-heading-xxl" data-purpose="course-price-text">
<span class="udlite-sr-only">Current price</span>
<span>Free</span>
</div>
And this is how I was scraping it with the help of JSOUP
Document document = Jsoup.connect(mainUrl).userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36").get();
String price = document.select("div.price-text--price-part--Tu6MH").first().text();
if (!price.contains("Free")) {
System.out.println("\nCoupon Expired...");
} else {
further code...
}
But, now I'm not able to, I did lot of R&D but still unable to do it. Can someone please help me with it?
I want is to get the FREE value.
Here is a example code which should help you. I have used the Jsoup.parse but you can use Jsoup.connect and use the code
String price = doc.select("div.price-text--price-part--Tu6MH").select("span:nth-last-child(1)").text()
or
String price = doc.select("div.price-text--price-part--Tu6MH > span:nth-last-child(1)").text();
String html = "<html><body><div class=\"price-text--price-part--Tu6MH udlite-clp-discount-price udlite-heading-xxl\" data-purpose=\"course-price-text\">\r\n" +
" <span class=\"udlite-sr-only\">Current price</span>\r\n" +
" <span>Free</span>\r\n" +
"</div></body></html>";
Document doc = Jsoup.parse(html);
String price = doc.select("div.price-text--price-part--Tu6MH > span:nth-last-child(1)").text();
System.out.println(price);
if (!price.contains("Free")) {
System.out.println("\nCoupon Expired...");
} else {
System.out.println("Its free");
}
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.