How do I extract a text from a website(html)

Viewed 38

I'm trying to get the value (38.08%) from the yahoo finance.

What I have so far is this:

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3'}
reg_url = 'https://finance.yahoo.com/quote/DIS/analysis?p=DIS'
req = Request(url=reg_url, headers=headers) 
soup = BeautifulSoup(requests.get(reg_url,headers=headers).content,'html.parser')

https://finance.yahoo.com/quote/DIS/analysis?p=DIS

location of the required value

Thanks in advance

2 Answers

To extract the html for the text you want to

  1. Highlight the text you want to copy by clicking and dragging with your mouse
  2. right click on the highlighted text
  3. Click inspect on the menu that comes up

this is what it should look like

https://i.stack.imgur.com/CI1ga.png

Hopefully this helps you

You could use the following approach:

from bs4 import BeautifulSoup
import requests

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3'}
reg_url = 'https://finance.yahoo.com/quote/DIS/analysis?p=DIS'
soup = BeautifulSoup(requests.get(reg_url,headers=headers).content,'html.parser')

for td in soup.select('td[class="Ta(start) Py(10px)"]'):
    if 'Next 5 Years' in td.text:
        print(td.next_sibling.text)

Which displays:

38.08%

I also suggest you investigate the Python Yahoo finance library.

Related