Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

beautifulsoup - 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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...