簡體   English   中英

雅虎財經下載特定數據python PE

[英]Yahoo Finance Download Specific Data python PE

嘗試在 Yahoo 中抓取一些基本的基本數據。 我以前可以用 BeautifulSoup 做到這一點。 不知何故它不起作用。 是時候使用Selenium了嗎?

url = https://finance.yahoo.com/quote/2638.HK/key-statistics?p=2638.HK假設我想獲取數字“尾隨市盈率”或“股本回報率(ttm)”或“任何領域”我該怎么做?

你仍然可以使用 BeautifulSoup。

from bs4 import BeautifulSoup
from requests import get

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0"
}

url = 'https://finance.yahoo.com/quote/2638.HK/key-statistics?p=2638.HK'
response = get(url, url, headers=headers)

soup = BeautifulSoup(response.text, 'html.parser')


data = soup.find_all("table")

def grab_by_argument(table_array, parameter):
    for table in table_array:
        trs = table.find_all('tr')
        for tr in trs:
            tds = tr.find_all('td')
            if parameter.lower() in tds[0].get_text().lower():
                return(tds[1].get_text())


print(grab_by_argument(data, "Trailing P/E"))

如果您有多個用於抓取的參數,只需將它們添加到列表中並對其進行迭代:

args = ["Trailing P/E", "Return on Equity (ttm)"]

for arg in args:
    print(grab_by_argument(data, arg))

如果您需要獲取所有股票數據:

def grab_all(stock_data):
    for table in stock_data:
        trs = table.find_all('tr')
        for tr in trs:
            tds = tr.find_all('td')
            print("Measure: {}".format(tds[0].get_text()))
            print("Value: {}".format(tds[1].get_text()))
            print("")

然后后者調用它:

grab_all(data)

您還可以將所有信息作為表格返回而不打印。

暫無
暫無

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

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