此 python脚本可以写 .html的内容(在本例中是写在“simple_website.html”),并显示从mysql中提取的数据
- .py
import mysql.connector
import webbrowser
import time
mydb = mysql.connector.connect(
host="196.100.100.141",
user="root",
password="password456",
database="database_db",
auth_plugin='mysql_native_password'
)
mycursor = mydb.cursor()
mycursor.execute("SELECT row_01 FROM first_db")
myresult = mycursor.fetchall()
list_01 = []
for row in myresult:
temp_val = row[0]
list_01.append(temp_val)
print(list_01[5]) # get specific data from MySQL
html_content = f"<html>this is the data extract from mysql:{list_01[5]}
</html>"
with open("simple_website.html","w") as html_file:
html_file.write(html_content) # show specific data on website
time.sleep(2)
webbrowser.open_new_tab("simple_website.html")
- 根据这个逻辑输出的网站截图
Q:如果我只想插入/替换 “(从mysql) 提取的数据”到特定网站位置 (ex: 标题或文本内容),我该怎么做?
想法:我把原来的 html 代码插入 html_content = f"
里,使其能在python 里work
旧的内容替换为 {list_01[5]}
我从mysql抓的数据,它应该可以正常工作,如上面的逻辑所示,我需要更改什么 对于代码
我做了
1.将原网站html放入.py脚本中的html_content = f"
2. 在第 109 行用 提取的 mysql data {list_01[5]}
替换旧内容
- 尝试的 .py 脚本
import mysql.connector
import webbrowser
import time
mydb = mysql.connector.connect(
host="196.100.100.141",
user="root",
password="password456",
database="database_db",
auth_plugin='mysql_native_password'
)
mycursor = mydb.cursor()
mycursor.execute("SELECT row_01 FROM first_db")
myresult = mycursor.fetchall()
list_01 = []
for row in myresult:
temp_val = row[0]
list_01.append(temp_val)
print(list_01[5]) # get specific data from MySQL
html_content = f"<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Rayson Technology - Your best OEM/ODM partner for Bluetooth solution</title>
<meta name="description" content=" content, we are the best company">
<meta name="keywords" content="content, where thestory begin" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="../images/favicon.png">
<link rel="stylesheet" href="../assets/css/vendor/bootstrap.min.css">
<link rel="stylesheet" href="../assets/css/vendor/cerebrisans.css">
<link rel="stylesheet" href="../assets/css/vendor/fontawesome-all.min.css">
<link rel="stylesheet" href="../assets/css/vendor/linea-icons.css">
<link rel="stylesheet" href="../assets/css/plugins/swiper.min.css">
<link rel="stylesheet" href="../assets/css/plugins/animate-text.css">
<link rel="stylesheet" href="../assets/css/plugins/animate.min.css">
<link rel="stylesheet" href="../assets/css/plugins/lightgallery.min.css">
<link rel="stylesheet" href="../assets/css/Custom.css">
<link rel="stylesheet" href="../assets/css/style.css">
</head>
<body>
<div class="preloader-activate preloader-active open_tm_preloader">
<div class="preloader-area-wrap">
<div class="spinner d-flex justify-content-center align-items-center h-100">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
</div>
<div class="header-bottom-wrap header-sticky">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="header default-menu-style position-relative">
<div class="header__logo">
<a href="index.html">
<img src="../images/logo.png" width="160" height="48" class="img-fluid" alt="">
</a>
</div>
<div class="header-midle-box">
<div class="header-bottom-wrap d-md-block d-none">
<div class="header-bottom-inner">
<div class="header-bottom-left-wrap">
<div class="header__navigation d-none d-xl-block">
<nav class="navigation-menu primary--menu">
<ul>
<li class="has-children has-children--multilevel-submenu">
<a href="product_list.html"><span>Products</span></a>
<ul class="submenu">
<li><a href="product_nodic.html"><span>Nordic</span></a></li>
<li><a href="product_microchip.html"><span>{list_01[5]}</span></a></li>
<li><a href="product_dk.html"><span>product_03</span></a></li>
</ul>
</li>
<li>
<a href="software_support.html"><span>Software Support</span></a>
</li>
<li>
<a href="customizations.html"><span>Customization</span></a>
</li>
<li>
<a href="success_stories.html"><span>Success Stories</span></a>
</li>
<li>
<a href="contact.html"><span>Contact</span></a>
</li>
</ul>
</nav>
</div>
</div>
</div>
</div>
</div>
<div class="header-right-box">
<div class="header-right-inner" id="hidden-icon-wrapper">
<div class="language-menu">
<ul>
<li>
<a href="#" class="">
<span class="wpml-ls-native">English</span>
</a>
<ul class="ls-sub-menu">
<li class="">
<a href="../chinese/" class="">
<span class="wpml-ls-native">繁體中文</span>
</a>
</li>
<li class="">
<a href="../schinese/" class="">
<span class="wpml-ls-native">簡體中文</span>
</a>
</li>
</ul>
</li>
</ul>
</div>
<div class="header-search-form default-search">
<form action="search.html" class="search-form-top">
<input class="search-field" type="text" placeholder="Search…">
<button class="search-submit">
<i class="search-btn-icon fa fa-search"></i>
</button>
</form>
</div>
</div>
<div class="footer-copyright-area section-space--ptb_15">
<div class="container-fluid">
<div class="row align-items-center">
<div class="col-md-6 text-center text-md-start">
<ul class="breadcrumb-list">
<li class="breadcrumb-item"><a href="index.html">Home</a></li>
<li class="breadcrumb-item"><a href="product_list.html">Products</a></li>
<li class="breadcrumb-item active">Nordic</li>
</ul>
</div>
<div class="col-md-6 text-center text-md-end">
<span class="copyright-text">© 2022 sam_JR Technology co.,Ltd. All Rights Reserved.</span>
</div>
</div>
</div>
</div>
</div>
</body>
</html>"
with open("simple_website.html","w") as html_file:
html_file.write(html_content) # show specific data on website
time.sleep(2)
webbrowser.open_new_tab("simple_website.html"
应该是相同的逻辑,只需要改进 try .py 代码的布局/格式
请问专家该如何修改代码格式
[1]: https://i.stack.imgur.com/4Q63c.png