0

How to use the BeautifulSoup to decode page which is JS ?

buf = requests.get() soup = BeautifulSoup(buf,"html.parser")

  1. when decoding "theglobeandmail.com/investing/markets/stocks/XDV-T", it is working, all the data available in "soup"

  2. when decoding "money.tmx.com/quote/BNS", only some info is available in "soup". When print the "buf" line by line, I noticed it is embedded by JS files.

2 Answers 2

1

Your issue derives from the fact that BeautifulSoup can only parse the HTML that you get from the initial request. In the second example, tmx.com is requesting a separate file (in this case https://app-money.tmx.com/graphql) that contains the price information, which is why it doesn't appear in your BeautifulSoup request. You can see this by opening the Inspect Developer tools tab by pressing F12 and navigating to the Network tab:

Network requests.

In order to get the price information, you'll need to send a request to https://app-money.tmx.com/graphql instead of https://money.tmx.com/quote/BNS with the appropriate headers indicating which stock you're requesting.

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

1 Comment

how do you locate that "graphql" is being used to get stock data ? As there is another web site which I have similar issue. Wonder how is the process to search for such info.
0

Thanks for @Brooke information.

Got it working with

Host="https://app-money.tmx.com/graphql"
Payload = {
        "operationName": "getQuoteBySymbol",
        "variables": 
        {
            "symbol": "BNS",
            "locale": "en"
        },
        "query": "query getQuoteBySymbol($symbol: String, $locale: String) {\n getQuoteBySymbol(symbol: $symbol, locale: $locale) {\n symbol\n name\n price\n priceChange\n percentChange\n }\n}"
    }

    response = requests.post(Host, json=Payload)

Comments

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.