0

How to parse Yahoo website in android for loser, Gainer & Most Active give me share any ideas or example My not working code are given below

try {
    Document mBlogDocument =  Jsoup.connect("https://sg.finance.yahoo.com/most-active").userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6").get();

    // done for etfs   Jsoup.connect("https://in.investing.com/etfs/india-etfs").timeout(10000).userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6").get().getElementById("etfs").select("tr").iterator();
    Elements links = mBlogDocument.getElementsByTag("a");
    // Locate the content attribute

} catch (IOException e) {
    e.printStackTrace();
}
Paul Chu
  • 1,249
  • 3
  • 19
  • 27

1 Answers1

1

I'm able to extract data from Yahoo website so make sure -

  1. Add <uses-permission android:name="android.permission.INTERNET" /> in AndroidManifest.xml
  2. Jsoup operation is in background thread

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // ....
        new MyTask().execute();
    }
    
    
    private class MyTask extends AsyncTask<Void, Void, String> {
    
        @Override
        protected String doInBackground(Void... params) {
            String title = "";
    
            try {
                Document mBlogDocument = Jsoup.connect("https://sg.finance.yahoo.com/most-active").userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6").get();
                Elements links = mBlogDocument.getElementsByTag("a");
                title = mBlogDocument.title();
                for (Element l: links) {
                    Log.d("log","Element <a>: " + l);
                }
                return title;
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return title;
        }
    
    
        @Override
        protected void onPostExecute (String result){
            // perform UI stuff
            // ((TextView)findViewById (R.id.textview)).setText (result);
        }
    }
    

Below is the code for extracting Matching Stocks data cell

private class MyTask extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        String title = "";
        String Symbol = "";
        String Name = "";
        String Price = "";
        String Change = "";
        String Change_percentage = "";
        String Volume = "";
        String Avg_Volume = "";
        String Market_cap = "";
        String PE_ratio = "";

        try {


            Document mBlogDocument = Jsoup.connect("https://sg.finance.yahoo.com/most-active").userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6").get();
            Elements tbody_table = mBlogDocument.select("tbody[data-reactid$=73]");
            Log.d("log", "tbody_table size: " + tbody_table.size());

            title = mBlogDocument.title();
            System.out.print(title);

            Elements Stocks = tbody_table.select("tr");
            for (Element Stock : Stocks) {
                Elements Stock_item = Stock.select("td");
                Symbol = Stock_item.get(1).text();
                Name = Stock_item.get(2).text();
                Price = Stock_item.get(3).text();
                Change = Stock_item.get(4).text();
                Change_percentage = Stock_item.get(5).text();
                Volume = Stock_item.get(6).text();
                Avg_Volume = Stock_item.get(7).text();
                Market_cap = Stock_item.get(8).text();
                PE_ratio = Stock_item.get(9).text();

                Log.d("log","Symbol: " + Symbol);
                Log.d("log","Name: " + Name);
                Log.d("log","Price: " + Price);
                Log.d("log","Change: " + Change);
                Log.d("log","Change_percentage: " + Change_percentage);
                Log.d("log","Volume: " + Volume);
                Log.d("log","Avg_Volume: " + Avg_Volume);
                Log.d("log","Market_cap: " + Market_cap);
                Log.d("log","PE_ratio: " + PE_ratio);
            }


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

        return title;
    }


    @Override
    protected void onPostExecute(String result) {
        //if you had a ui element, you could display the title
        //   ((TextView)findViewById (R.id.myTextView)).setText (result);
    }
}

Keep in mind that there are 1114 Matching Stocks results and all I can get is 250 records per URL due to some protection (https://sg.finance.yahoo.com/most-active?offset=0&count=250). So you can do some looping for below URLs if you want to retrieve ALL Matching Stocks:

https://sg.finance.yahoo.com/most-active?offset=0&count=250 https://sg.finance.yahoo.com/most-active?offset=250&count=250 https://sg.finance.yahoo.com/most-active?offset=500&count=250 https://sg.finance.yahoo.com/most-active?offset=750&count=250 https://sg.finance.yahoo.com/most-active?offset=1000&count=114

Paul Chu
  • 1,249
  • 3
  • 19
  • 27
  • i want to fetch like Symbol, Name, Price (Intraday), Change % , Change Volume, Avg Vol (3 month), Market Cap,PE Ratio (TTM) Please help me Paul if u can – Anup Singh Apr 01 '18 at 07:53
  • You're welcome:) Try dig deep into HTML/Javascript, it helps with data extraction – Paul Chu Apr 01 '18 at 15:31
  • Hi @PaulChu, Can you help me. i want to implement google finance search api but not working properly some times show data some times not. i shared URL please Check it " http://www.google.com/finance/match?matchtype=matchall&q=" – Anup Singh Apr 07 '18 at 12:13
  • I redirect to [this](https://www.google.com/search?q=%22&tbm=fin) page based on the URL you post, and as far as I can tell Google API normally have [certain limit calls per day](https://stackoverflow.com/questions/35939894/limit-of-multiple-quote-in-a-finance-google-api-call) – Paul Chu Apr 07 '18 at 14:34
  • And you should [create a new question](https://stackoverflow.com/help/how-to-ask) for this since it is not relevant to this post – Paul Chu Apr 07 '18 at 14:40
  • Yes, But Currently working on this app https://play.google.com/store/apps/details?id=com.kuchkaapps.worldstockmarket.gf – Anup Singh Apr 07 '18 at 17:14
  • Hi Paul, i post new problem please resolve this https://stackoverflow.com/questions/49828523/how-to-proper-working-with-asynctask-in-android-for-fetch-data-from-tricker – Anup Singh Apr 14 '18 at 06:30
  • https://stackoverflow.com/questions/76642681/getcrumb-in-kotlin-not-getting-crumb-value Hi Paul can you resolve this issue. Thanks in Advance – Anup Singh Jul 08 '23 at 11:29