簡體   English   中英

Python 雅虎財經-數據抓取

[英]Python Yahoo Finance - data scraping

我是編程新手,我只是想獲得“目標價格:來自雅虎金融的價值我試過 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)

打印所有跨度時,它不顯示...

我開發了以下示例來抓取您感興趣的數據。這些數據特別難以獲得,因為只有在頁面向下滾動足夠多時才由 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM