使用 Python 进行 WebScraping 不会更新值

问题描述 投票:0回答:0

我一直在尝试获取输入的股票代码的股息价值,并使用网络抓取和 python 从雅虎金融将它们添加到字典中。 但是,我得到的输出不正确。

d = {}
for company in valid_tickers:
    print(company)
    # Make a request to the website
    url = "https://finance.yahoo.com/quote/" + company + "/key-statistics?p=" + company  
    print(url)
    response = requests.get(url, headers=headers).text
    
    soup = BeautifulSoup(response, 'html.parser')
    
    # Find all the tables in the HTML
    tables = soup.find_all('table')

    # Loop through the tables and extract the data
    for table in tables:
        # Find all the rows in the table
        rows = table.find_all('tr')

        # Loop through the rows and extract the data
        for row in rows:
            # Find all the cells in the row
            cells = row.find_all('td')

            # Loop through the cells and append the data to a list

            for cell in cells:
                table_values.append(cell.text)

    dividend_index = table_values.index("Forward Annual Dividend Yield 4")
    

    dividend = table_values[dividend_index + 1]

    print(dividend)

    d[company] = dividend

print(d)

如果输入是:AAPL,TSLA。我应该得到:AAPL,0.56% 和 TSLA,N/A

目前股息价值没有改变,并保持为输入的第一个代码。

如果输入是AAPL,TSLA,当前输出是: 苹果,0.56% 特斯拉,0.56%

python yahoo-finance
© www.soinside.com 2019 - 2024. All rights reserved.