简体   繁体   中英

Python Yahoo Finance - data scraping

i'm new to programming and i'm just trying to get "Target price: value from yahoo finance i tried beautifulsoup, xpath...but never succeed to extract the data (241.21 $ ) on the following example

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

Target price area

import requests
from bs4 import BeautifulSoup as bs
ticker = 'MSFT'
url ='https://finance.yahoo.com/quote/'+Symbol
page = requests.get(url)
soup = bs(page.text, "html.parser")
for row in table:
    col = row.find_all('span')
    for c in col:
        print(c.text)

When printing all spans, it doesnt show up...

I've developed the following example to scrape the data you are interested in. This data was particularly difficult to get as it was filled in by javascript only once the page has scrolled down enough.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
    
driver = webdriver.Chrome()
driver.get('https://finance.yahoo.com/quote/MSFT/analysis?p=MSFT')

# Scroll down to the container of the data. The Analyst Price Targets div.
# The needed data is not filled in until scrolled down
driver.execute_script("""document.querySelector('div#Aside div[data-reactid="48"]').scrollIntoView();""")

# Get the span that contains the data
elem = driver.find_element_by_xpath("//div[@data-test='analyst-avg-tg']//span[2]")
print(elem.text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM