Python BeautifulSoup4[]输出错误
我正在做一个项目,在这个项目中,我使用beautifulsoup4获取网站的某些部分。我正在使用网站“”,我想取显示为当前股价的大数字。当我检查这个数字的元素时,我得到:Python BeautifulSoup4[]输出错误,python,beautifulsoup,Python,Beautifulsoup,我正在做一个项目,在这个项目中,我使用beautifulsoup4获取网站的某些部分。我正在使用网站“”,我想取显示为当前股价的大数字。当我检查这个数字的元素时,我得到: <span class="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" data-reactid="35">186.31</span> 当我打印“数字”时,我得到输出[]。我做错了什么 非常感谢 以下是完整的代码供参考: from tkinter import
<span class="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" data-reactid="35">186.31</span>
当我打印“数字”时,我得到输出[]。我做错了什么
非常感谢
以下是完整的代码供参考:
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)
当我单击按钮“提交”时,问题就出现了,这意味着当它运行函数“Calculation”时,这甚至不是有效的语法。请显示您使用的实际代码。以下是完整代码:page=requests.get(url)soup=BeautifulSoup(page.content,“lxml”)numbers=soup.find_all(“div”)print(numbers)注释中不提供详细信息,请编辑您的answer@averroes我没有。如何将列表格式化为字符串?我在这里至少算错了三件事。1) 无效语法2)当您需要span时搜索div 3)搜索类而不是属性4)注释中的代码与问题中的代码不匹配
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)