導入包
import os.path
import pandas as pd
import numpy as np
# 構造一個DataFrame對象
df = pd.DataFrame(np.random.random([5,5]), index=['a','b','c','d','e'], columns=['aa','bb','cc','dd','ee'])
# 生成html文件
fpath = r'C:\Users\Public'
fName = 'pandas_html.html'
# df.to_html(os.path.join(fpath,fName))
# 定義列表
strs = ['<HTML>'] # 'html開始標簽
strs.append('<HEAD><TITLE>to_html</TITLE></HEAD>') # html的標題標簽
strs.append('<BODY>') # 'body開始標簽
strs.append(df.to_html())
strs.append("</BODY></HTML>") # 結束標簽
# 把列表元素鏈接成字符串
html = "".join(strs)
# 字符串寫入html文件
file = open(os.path.join(fpath,fName), 'w')
file.write(html)
file.close()
# 讀取html文件
# read_html讀取的結果是一個DataFrame的list
fullpath = os.path.join(fpath, fName)
print(fullpath)
df = pd.read_html(fullpath)
print(df[0])
# 從網頁讀取table數(shù)據(jù)
webpage = 'https://....'
df1 = pd.read_html(webpage)
print(df1[0])
print(df1[1])
TML常用標簽有:a標簽、table標簽、img標簽、form標簽和input標簽。
作用
屬性
(一)href
href是hyper reference的縮寫,超鏈接的意思。
用于指定鏈接目標的ur地址,(必須屬性)當為標簽應用href屬性時,它就具有了超鏈接的功能;
href=“#”表示這是一個空鏈接;
如果href里面地址是—個文件或者壓縮包,會下載這個文件。
<a href="https://google.com">超鏈接到google網站的主頁</a>
<a href="https://google.com">超鏈接到google的主頁</a>
<a href="//google.com">超鏈接到google的主頁</a>
展現(xiàn)形式:
點擊此鏈接,即可到達google的主頁
a標簽href的取值:
1、上述代碼中的網址的取值(推薦使用第三行的代碼)
<a href="//google.com">超鏈接到google的主頁</a>
由于此方式能夠自動補齊相關的網絡地址,前面兩種寫錯就會報錯,所以推薦使用。
2、路徑
當前路徑下的a里面的b,b里面的c
在當前目錄下尋找index.html文件
3、偽協(xié)議
<a href="javascript:;">點擊后無任何點擊或刷新等動作的反應</a>
<a href="#要跳轉的元素的id"></a>
點擊鏈接的時候,會跳轉到指定元素所在的位置。
<a href="mailto:abcdefg@163.com ">發(fā)郵件給我</a>
<a href="tel:12345678901">打電話給我</a>
(二)targe
用于指定鏈接頁面的打開方式
a的target取值
1、內置名字
_blank 在空白頁打開
_self 在當前頁面打開
_parent 在父級窗口打開
_top 在最頂級的窗口打開
<a href="//google.com" target="_blank">超鏈接到google網站的主頁在空白頁打開</a>
2、程序員的命名
window:name(在xxx頁面打開)
iframe的name(iframe現(xiàn)在已經很少使用了,是指內嵌窗口)
(三)download
下載頁面,但目前很少用,有的瀏覽器不支持,尤其是手機瀏覽器可能不支持。
1、table標簽的語法:
thead:表頭
tbody:表的內容,用于定義
tfoot:表的腳部
tr:table row,表格里的行
th:表格的表頭部分,其中的文本內容字體加粗居中顯示
td:table data,表格數(shù)據(jù),用于定義表格中的單元格
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<table>
<thead>
<tr>
<th></th>
<th>小紅</th>
<th>小黃</th>
<th>小藍</th>
</tr>
</thead>
<tbody>
<tr>
<th>數(shù)學</th>
<td>90</td>
<td>60</td>
<td>80</td>
</tr>
<tr>
<th>語文</th>
<td>88</td>
<td>95</td>
<td>97</td>
</tr>
<tr>
<th>英語</th>
<td>88</td>
<td>95</td>
<td>97</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>總分</th>
<td>266</td>
<td>250</td>
<td>274</td>
</tr>
</tfoot>
</table>
</body>
</html>
第一行的th標簽為空
2、table的樣式
table-layout:auto;自動計算每一行的寬高
table-layout:fixed;通過列表的寬度來決定平均寬度
border-collapse:collapse; 合并邊框(默認邊框與邊框之間不合并)
border-spacing:0;邊框為0.(邊框與邊框之間的距離)。
作用:發(fā)出get請求,展示一張圖片。
<img src="1.JPG" alt="頭像" width="400" />
當前路徑下的1.jpg,確定寬度為400,只寫寬度高度會自適應
屬性
alt:alternate的縮寫,替換的意思。替換文本,圖像不能顯示的文字。
路徑錯誤顯示alt內容
title:提示文本。鼠標放到圖像上,顯示的文字。
響應
max-width:100% 所有的圖片在手機上都自適應寬度,寬度最大為100%。
事件
onload/onerror 監(jiān)聽圖片是否加載成功,加載成功時用onload,不成功是用onerror事件。確保在onerror事件能夠補救。
<body>
<img id="xxx" src="dog.jpg" alt="一只小狗">
<script>
xxx.onload = function () {
console.log("圖片加載成功");
};
xxx.onerror = function () {
console.log("圖片加載失敗");
xxx.src = "/404.jpg";
};
</script>
</body>
監(jiān)聽成功時,打印出成功
監(jiān)聽失敗時,先打印出監(jiān)聽失敗并且開始執(zhí)行加載失敗是的挽救圖片。404.jpg文件執(zhí)行
本文為作者本人的原創(chuàng)文章,著作權歸作者本人和饑人谷所有,轉載務必注明來源。
下是使用 C# 創(chuàng)建 HTML 文件的代碼示例:
using System;
using System.IO;
namespace CreateHTMLFile
{
class Program
{
static void Main(string[] args)
{
// 設置 HTML 文件路徑及文件名
string filePath = @"C:\Temp\example.html";
// 創(chuàng)建一個新的 HTML 文件
using (StreamWriter writer = new StreamWriter(filePath))
{
// 寫入 HTML 標記
writer.WriteLine("<html>");
writer.WriteLine("<head><title>Example HTML File</title></head>");
writer.WriteLine("<body>");
// 寫入主體內容
writer.WriteLine("<h1>Hello, World!</h1>");
writer.WriteLine("<p>This is an example HTML file created using C#.</p>");
// 寫入 HTML 結束標記
writer.WriteLine("</body>");
writer.WriteLine("</html>");
}
// 輸出成功消息
Console.WriteLine("HTML 文件已創(chuàng)建:{0}", filePath);
}
}
}
此代碼將創(chuàng)建一個名為 “example.html” 的 HTML 文件,并在其中添加一些基本的元素。你可以根據(jù)需要修改或擴展它,以滿足你的具體需求。注意,要創(chuàng)建 HTML 文件,你需要使用 System.IO 命名空間中的 StreamWriter 類。該類允許你向文件寫入文本數(shù)據(jù)。
*請認真填寫需求信息,我們會在24小時內與您取得聯(lián)系。