Fetching proper text from html tags using JSoup

mara122 :

I want to fetch title from online bookstore. Unforunately it is fetching me something like this:

"title": "1 Jak mniej myśleć. Dla analizujących bez końca i wysoko wrażliwych"

I want to get rid of this "1", same for next books, because the next books will have 2,3,4 etc... So it should go with:

"title": "Jak mniej myśleć. Dla analizujących bez końca i wysoko wrażliwych"

I was wondering if it is possible with Jsoup or I need to think about only Java code.

Here is the fetching instruction that takes this title (I'm using for loop, that's why it looks like this)

document = Jsoup.connect(bestSellersEmpikURL).get();
List<Element> siteElements = document.select("div.productWrapper");
 for (int i = 0; i < 5; i++) {
           ....
            String title = siteElements.get(i).select("strong").first().text();
            ...
}

Here is the site:

<a href="/za-duzo-mysle-poradnik-dla-analizujacych-bez-konca-petitcollin-christel,p1222736270,ksiazka-p" class="img seoImage" title="Jak mniej myśleć. Dla analizujących bez końca i wysoko wrażliwych &nbsp;-&nbsp;Petitcollin Christel" rel="nofollow" data-product-id="p1222736270">
<img class="lazy" src="https://ecsmedia.pl/c/jak-mniej-myslec-dla-analizujacych-bez-konca-i-wysoko-wrazliwych-p-iext54318159.jpg" lazy-img="https://ecsmedia.pl/c/jak-mniej-myslec-dla-analizujacych-bez-konca-i-wysoko-wrazliwych-p-iext54318159.jpg" alt="">
</a>
    <div class="name">
<a href="/za-duzo-mysle-poradnik-dla-analizujacych-bez-konca-petitcollin-christel,p1222736270,ksiazka-p" class="seoTitle" title="Jak mniej myśleć. Dla analizujących bez końca i wysoko wrażliwych &nbsp;-&nbsp;Petitcollin Christel" data-product-id="p1222736270">
<strong class="ta-product-title"><span class="blue-number">1</span>
Jak mniej myśleć. Dla analizujących bez końca i wysoko wrażliwych
</strong>
</a>
<div class="smartAuthorWrapper ta-product-smartauthor">
<a href="/szukaj/produkt?author=petitcollin+christel" class="smartAuthor" title="Petitcollin Christel - wszystkie produkty">
Petitcollin Christel </a>
</div>
<div class="categoryAndRatings">
<div class="category">
<span>
<span class="productMainInfoSuffix ta-product-category">Książki</span>
|
<span class="productMainInfoSuffix ta-product-carrier">
okładka&nbsp;miękka
</span>
</span>
</div>
<div class="rating">
<ul class="ratingStars">
<li class="rate">
<i class="fa fa-fw fa-star"></i>
...

and link https://www.empik.com/bestsellery/ksiazki

Hovercraft Full Of Eels :

Then your problem may be solvable by simply changing

siteElements.get(i).select("strong").first().text();

to

siteElements.get(i).select("strong").first().ownText();

or more briefly, changing .text() to .ownText().

This will display the Element's own text and not nested element text.

e.g.,

document = Jsoup.connect(bestSellersEmpikURL).get();
List<Element> siteElements = document.select("strong.ta-product-title");
for (Element element : siteElements) {
    System.out.println(element.ownText());
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=154763&siteId=1