首页 > 解决方案 > Python Yahoo Finance - 数据抓取

问题描述

我是编程新手,我只是想获得“目标价格:来自 yahoo Finance 的价值我尝试了 beautifulsoup、xpath ......但在以下示例中从未成功提取数据 (241.21 $)

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

目标价区域

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)

打印所有跨度时,它不显示...

标签: pythonbeautifulsoup

解决方案


我开发了以下示例来抓取您感兴趣的数据。这些数据特别难以获取,因为只有在页面向下滚动足够多时才由 javascript 填充它。

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)

推荐阅读