-1

I'm trying to extract the text from an h2 tag in HTML through JSoup, but for some reason it's just not working. Any help would be appreciated, thanks!

HTML Code:

<div id="searchTemplate" class="searchTemplate listLayout so_us_en" >
                <div id="topDynamicContent">
                        <div id="s-result-info-bar" class="a-row a-spacing-base searchUndoAUIHacks"><div id="s-result-info-bar-content" class="a-row"><div class="a-column a-span8 a-spacing-none"><div class="s-first-column">
            <h2 id="s-result-count" class="a-size-base a-spacing-small a-spacing-top-small a-text-normal">1-16 of 121 results for <span><span class="a-color-state a-text-bold">&#034;rx 390&#034;</span></span></h2></div>

Java Code:

Document doc = Jsoup.connect("https://www.amazon.com/s/ref=nb_sb_ss_c_1_6?url=search-alias%3Daps&field-keywords=rx+390&sprefix=rx+390%2Caps%2C166&crid=2MTUBA4KGNY06").get();
        String link= doc.select("div.searchTemplate.listLayout.so_us_en")
                .select("div[id= topDynamicContent]")
                .select("div.a-row.a-spacing-base.searchUndoAUIHacks")
                .select("div.a-row")
                .select("div.a-column.a-span8.a-spacing-none")
                .select("div.s-first-column")
                .select("h2.a-size-base.a-spacing-small.a-spacing-top-small.a-text-normal").first().text();
coolyfrost
  • 145
  • 10
  • Please tell the details. What's not working? How? What are you seeing? What are you expecting? – Hovercraft Full Of Eels Aug 06 '17 at 23:16
  • You code works for me. The result is `1-16 of 121 results for "rx 390"`. –  Aug 06 '17 at 23:20
  • That's strange, that part of the code must be right but I'm righting an Android app so something else must not be working. I have no idea what though. Thanks for helping me narrow it down though! – coolyfrost Aug 06 '17 at 23:34

1 Answers1

0

As you told us in the comments, you are working on an Android app. Therefore there could be a few mistakes (which I experienced myself).

First, did you check, you are running on a seperate thread? Android is a little bit nasty with using Threads for each NetworkThread on the main UI. For reference see here: https://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

You could do something like this in your code:

Thread threadNet = new Thread() {
  public void run() {
    Document doc;
    try {
      doc = Jsoup.connect("https://www.amazon.com/s/ref=nb_sb_ss_c_1_6?url=search-alias%3Daps&field-keywords=rx+390&sprefix=rx+390%2Caps%2C166&crid=2MTUBA4KGNY06").get();
      String link= doc.select("div.searchTemplate.listLayout.so_us_en")
                .select("div[id= topDynamicContent]")
                .select("div.a-row.a-spacing-base.searchUndoAUIHacks")
                .select("div.a-row")
                .select("div.a-column.a-span8.a-spacing-none")
                .select("div.s-first-column")
                .select("h2.a-size-base.a-spacing-small.a-spacing-top-small.a-text-normal").first().text();
          System.out.print(link);
    } catch (IOException e) {
          e.printStackTrace();
    }
  }
};
threadNet.start();

Nonetheless, as mentioned by @saka1029, the code is generally correct and working fine under Windows or Linux systems.

r3dst0rm
  • 1,876
  • 15
  • 21
  • Yeah, the issue was that I was not running on a separate thread. Thanks for your help! Also, what is the difference between running a thread the way that you wrote above compared to extending AsyncTask within a class? What are the advantages/disadvantages for each one? – coolyfrost Aug 09 '17 at 00:40
  • Basically a AsyncTask allows you to update UI components. Which you can't out of the box using a Thread. In order to update the UI inside a Thread you'll need a Handler. An extended explanation is available here: https://stackoverflow.com/a/25848392 – r3dst0rm Aug 09 '17 at 05:26
  • 1
    Alright, thank you so much for your help. – coolyfrost Aug 09 '17 at 22:34