为什么 Python Web 应用程序抛出 *AttributeError:'str'对象没有属性 'Div'*?



我正在构建一个python web应用程序,以在时间序列图中显示金价走势。但我的代码抛出了一个错误,说

app.layout=html。Div([AttributeError:"str"对象没有属性"Div">

这是代码

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input,Output
from urllib.request import urlopen, Request
url = "http://goldpricez.com/gold/history/lkr/years-3"
req = Request(url=url)
html = urlopen(req).read().decode('utf-8')
df = pd.read_html(url)  # this will give you a list of dataframes from html
df1 = df[3]
first_val = df1.iloc[0][0]

date = df1[0]
price = df1[1]
data = [df1[0],df1[1]]
headers = ["Date", "Price"]
df3 = pd.concat(data, axis=1, keys=headers)

from datetime import datetime
df3['Date'] = df3['Date'].apply(lambda x: datetime.strptime(x, '%d-%m-%Y'))

df3['Year'] = pd.DatetimeIndex(df3['Date']).year
df3['Month'] = pd.DatetimeIndex(df3['Date']).month
df3['Day'] = pd.DatetimeIndex(df3['Date']).day
df3['WDay'] = df3['Date'].dt.dayofweek
df3['WeekDayName'] = pd.DatetimeIndex(df3['Date']).day_name()

print(df3['WDay'])

writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df3.to_excel(writer, sheet_name='Sheet1')
# Close the Pandas Excel writer and output the Excel file.
writer.save()

app = dash.Dash(__name__)

app.layout = html.Div([
html.H1("Gold Price Analyst" , style={'text-align' : 'center'}),
dcc.Graph(id='this_year_graph', figure={})

])
@app.callback(
[Output(component_id='this_year_graph', component_property='figure')]
)
def update_graph():
dff = df3.copy()
dff = [df4['Date'],df4['Price']]
fig = px.line(dff , x=dff['Date'], y=dff['Price'])
return fig
if __name__ =='__main__' :
app.run_server(debug=True)

在这里,我也添加了html=urlopen(req(.red((.decode('utf-8'(。我是python的新手,需要帮助来解决这个问题。

当您分配给html时,

html = urlopen(req).read().decode('utf-8')

覆盖具有相同名称的html导入

import dash_html_components as html

尝试将该html变量重命名为类似html_content、的其他变量

html_content = urlopen(req).read().decode('utf-8')