Search code examples
javaparsingweb-scrapingjsoup

How can I parse specific table data using jsoup?


I am developing a small project in which I'm trying to parse updated market prices for the crops from the table on this link:
http://amis.pk/ViewPrices.aspx?searchType=1&commodityId=1

I want to get the output like, Apple(ammre):12500

The code I'm using is:

public class MainActivity extends AppCompatActivity {
    private String url="http://amis.pk/ViewPrices.aspx?searchType=1&commodityId=1";
    TextView datatv;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        datatv=(TextView)findViewById(R.id.tv);
        btn=(Button)findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.CUPCAKE)
            @Override
            public void onClick(View view) {
               new Description().execute();
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.CUPCAKE)
    private class Description extends AsyncTask<Void, Void, Void> {
        StringBuilder s=new StringBuilder();
        String title;

        @Override
        protected Void doInBackground(Void... params) {
            try {

Document mBlogDocument = Jsoup.connect(url).get();

                Log.e("Activity Log", "doInBackground"+mBlogDocument.toString());
Elements table = mBlogDocument.getElementsByClass("table.cart");
Elements tdsInSecondRow = mBlogDocument.select("table tr:nth-child(2) > td");
                 for (Element td : tdsInSecondRow)
                {
                   System.out.println("TD: " + td.text());
                }
s.append(table);
                s.append(tdsInSecondRow);


            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

This code is returning me the complete html of table data in second row but how can i get only the data from 4th column (max price) specific to apple(ammre)? I don't have a clue about that. Any help will be appreciated.


Solution

  • This code gets all table rows and prints them one-by-one:

    Document document = Jsoup.connect(url).get();
    Elements rows = document.select("#amis_prices").select("tr:not(.labelLists)");
    for (Element row : rows) {
        String name = row.select(".listItem").text();
        String maxPrice = row.select(".pricedata:nth-of-type(3)").text();
        System.out.println(name + ": " + maxPrice); // or what is appropriate in your code
    }
    

    Please note that if you're coding for android, replace the last line System.out... with what is appropeiate to your code - e.g. button.setText(name + maxPrice) or...

    If you just want to get the second row, this is how you would do it:

    Document document = Jsoup.connect(url).get();
    Elements row = document.select("#amis_prices").select("tr:nth-of-type(2)"); // this 2 means the second row that you wanted
    String name = row.select(".listItem").text();
    String maxPrice = row.select(".pricedata:nth-of-type(3)").text();
    System.out.println(name + ": " + maxPrice); // or what is appropriate in your code