我正在开发一个项目,我使用beautifulsoup4来获取网站的某些部分。我正在使用网站“https://finance.yahoo.com/quote/AAPL/history?p=AAPL&guccounter=1”,我想将显示的大数字作为当前股票价格。当我检查这个号码的元素时,我得到:
<span class="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" data-reactid="35">186.31</span>
在我的程序中,我目前有代码:
soup = BeautifulSoup(page.content, "lxml")
numbers = soup.find_all("span", {"class"}: {"data-reactid"})
当我打印“数字”时,我得到输出[]。我错了什么?
非常感谢!
以下是完整的参考代码:
from tkinter import *
from bs4 import BeautifulSoup
import requests
import lxml
root = Tk()
root.title("Stock Price")
root.configure(background="white")
Calculator = Frame(root, height=300, width=500, bg="white").grid(column=0, row=2)
title = Label(root, bg="white", text="Stock Price Calculator", font="Sans 25 bold", fg="black").grid(row=0)
Stock_ticker = Label(root, text="Input stock ticker here:", font="Sans 18 bold")
Stock_ticker.place(x=7, y=60)
Ticker_entry = Entry(root, width=10)
Ticker_entry.place(x=235, y=64)
Stock_price = Label(root, text="Current stock price:", font="Sans 15")
Stock_price.place(x=7, y=100)
Stock_price_output = Entry(root, width=10)
Stock_price_output.place(x=160, y=100)
Stock_price_day = Label(root, text="Opening price for the day:", font="Sans 15")
Stock_price_day.place(x=7, y=140)
Stock_price_day_output = Entry(root, width=10)
Stock_price_day_output.place(x=195, y=141)
Stock_news = Label(root, text="News about stock:", font="Sans 15")
Stock_news.place(x=7, y=180)
Stock_news_output1 = Entry(root, width=30)
Stock_news_output1.place(x=150, y=181)
Stock_news_output2 = Entry(root, width=30)
Stock_news_output2.place(x=150, y=210)
Stock_news_output3 = Entry(root, width=30)
Stock_news_output3.place(x=150, y=239)
Submit = Button(root, text="Submit", font="Sans 14", command = lambda: Calculation())
Submit.place(x=165, y=270)
Reset = Button(root, text="Reset", font="Sans 14", command = lambda: Cleaning(Ticker_entry, Stock_price_output, Stock_price_day_output, Stock_news_output1, Stock_news_output2, Stock_news_output3))
Reset.place(x=250, y=270)
def make_url(ticker_symbol):
return "https://finance.yahoo.com/quote/%s/history?p=%s" % (ticker_symbol, ticker_symbol)
def Calculation():
stock = Ticker_entry.get()
url = make_url(stock)
page = requests.get(url)
soup = BeautifulSoup(page.content, "lxml")
numbers = soup.find_all("span", {"class": "data-reactid"})
print(numbers)
def Cleaning(writing_area1, writing_area2, writing_area3, writing_area4, writing_area5, writing_area6):
writing_area1.delete(0, END)
writing_area2.delete(0, END)
writing_area3.delete(0, END)
writing_area4.delete(0, END)
writing_area5.delete(0, END)
writing_area6.delete(0, END)
单击“提交”按钮时会出现问题,这意味着当它运行“计算”功能时