1

I'm trying to write an xpath query to import some content from a webpage in google spreadsheets using importXML function. I need to capture % of Buy under COMMUNITY SENTIMENTS on the below webpage: https://www.moneycontrol.com/india/stockpricequote/banks-public-sector/statebankindia/SBI

This % was showing 73% at time of posting this message, but may change later. (So I need to import 73% in my Google sheets).

Relevant HTML code of this page has below script:

</script>
        <ul class="buy_sellper">
            <li><span result="73" class="bullet_clr buy buy_results"></span>73% BUY</a></li>
            <li><span result="20" class="bullet_clr sell sell_results"></span>20% SELL</a></li>
            <li><span result="7" class="bullet_clr hold hold_results"></span>7% HOLD</a></li>
        </ul>
                    </div>
                </div>
                <div class="chart_fr ">

            <div class="txt_pernbd">73%</div>
            <div class="cht_mt25">of moneycontrol users recommend <span class=green_txt>buying</span> SBI</div>
                </div>
            <!-- buy, sell, hold starts -->
<div class="buy-sell-hold">
    <p>What's your call on SBI today?</p>
    <p>

Using Chrome, I used "inspect element" function and then "copy xpath" which gave me the following....

//*[@id="MshareElement"]

But this is not getting any results when I use in Google sheets with importxml function. I have zero knowledge of programming and I am trying to learn web scraping techniques using this function.

Please help.

2
  • Dear @Player0, Thank you very much for your response. I spent so many hours in understanding the syntax of importxml, and still failed to get the solution. And I never thought that answer would lie between multiple functions. I really appreciate your support. Commented May 7, 2020 at 2:37
  • Hi @player0, Can you please suggest how to import '500112' and 'SBIN' in Google spreadsheet from [link] (moneycontrol.com/india/stockpricequote/banks-public-sector/…). Also, please suggest how can I learn writing such xpath queries. Commented Aug 17, 2020 at 13:50

1 Answer 1

1

try:

=REGEXEXTRACT(QUERY(IMPORTDATA(
 "https://www.moneycontrol.com/india/stockpricequote/banks-public-sector/statebankindia/SBI"), 
 "where lower(Col1) contains 'txt_pernbd'"), ">(.+?)<")

0


=REGEXEXTRACT(QUERY(IMPORTDATA(
 "https://www.moneycontrol.com/india/stockpricequote/banks-public-sector/statebankindia/SBI"), 
 "where lower(Col1) contains 'bullet_clr sell sell_results'"), "/span>(.+?)</a")

0


=REGEXEXTRACT(QUERY(IMPORTDATA(
 "https://www.moneycontrol.com/india/stockpricequote/banks-public-sector/statebankindia/SBI"), 
 "where lower(Col1) contains 'investor views'"), ">(.+?)<")

0

Sign up to request clarification or add additional context in comments.

3 Comments

Can you also suggest how to extract sell% and the number of investors based upon which this % is extracted (both given under community sentiments section'. I know I may be asking for too much, but will really appreciate your help! :)
Hi @player0, Can you please suggest how to import '500112' and 'SBIN' in Google spreadsheet using importXML function/ xpath query? (moneycontrol.com/india/stockpricequote/banks-public-sector/…)
Hi @player0, will appreciate your help. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.