Python读取本地html文件内容存csv

直接上代码

import os
from html2text import html2text
import csv
file_path = './data'
file_names = os.listdir(file_path)
# 1. 创建文件对象
f = open('文件名3.csv','w',newline='',encoding='utf-8')
# 2. 基于文件对象构建 csv写入对象
csv_writer = csv.writer(f)
# 3. 构建列表头
csv_writer.writerows([["用户名", "解析"]])
for i in range(2):
    with open(os.path.join(file_path, file_names[i]), 'r', encoding='utf-8') as f1:
        txt = f1.read()
    #print(html2text(txt))
    jiexi=html2text(txt)
    yonghuming=file_names[i][0:2]
    # 4. 写入csv文件内容
    csv_writer.writerow([yonghuming,jiexi])
# 5. 关闭文件
f.close()
f1.close()
print("ok")

HTML内容如下

<!DOCTYPE html>
<html>
<head><meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta charset=”utf-8″/><title></title></head>
<body>
	<div style="margin:20px;"><h2><center>校内试卷</center></h2></div>
	<div style="margin:20px;"><h4></h4></div>
	<div style="margin:10px;background-color:#aaa"><h4>基础题</h4></div>
	<div style="margin:20px;">
		<div style="width:15px;float:left;">1.&nbsp;&nbsp;</div>
		<div style="margin-bottom:5px;margin-left:30px;"><br>
					1、将“备课”文件夹压缩,保存在:student文件夹<br>
					   压缩包命名为:上课.rar<br>
					<br>
					2、提取“亲亲.avi”中的声音(低质量)<br>
					   命名为:卖萌.mp3<br>
					   保存在:音频文件夹<br>
					<br>
					3、将“单击2.wav”移动到:音频文件夹<br>
					<br>
					4、删除:佩奇.avi<br>
					<br>
		</div>
		<div style="background-color:#eeee77;margin-left:40px;">得分:20 </div>
		<div style="background-color:#eeeeee;margin-left:40px;">
					解析:<br>
					0. 压缩(25) ---> 25<br>
					1. 提取声音(25) ---> 25<br>
					2. 文件的移动(25) ---> 25<br>
					3. 删除佩奇(25) ---> 25<br>
		</div>
 	</div>
 	<div style="overflow:hidden;clear:both;"></div>
 	<div style="margin:10px;background-color:#aaa"><h4>Word操作题</h4></div>
 	<div style="margin:20px;">
 			<div style="width:15px;float:left;">2.&nbsp;&nbsp;</div>
			<div style="margin-bottom:5px;margin-left:30px;"><br>
				1、将文中下划线改为:波浪线<br>
				<br>
				2、将表格上方三段(红色段落)设置:左对齐,左右各缩进2字符,段前间距1行<br>
				<br>
				3、将文章最后的艺术字设置:宋体,初号<br>
				<br>
				4、将表格中所有单元格设置为靠下居中对齐,并删除表格最后一行<br>
				<br>
			</div>
			<div style="background-color:#eeee77;margin-left:40px;">得分:20 </div>
			<div style="background-color:#eeeeee;margin-left:40px;">
				解析:<br>0. 波浪线(15) ---> 15<br>
				1. 左对齐(10) ---> 10<br>
				2. 左缩进(10) ---> 10<br>
				3. 右缩进(10) ---> 10<br>
				4. 段前间距(10) ---> 10<br>
				5. 艺术字的字体(10) ---> 10<br>
				6. 艺术字的字号(10) ---> 10<br>
				7. 表格水平居中(5) ---> 5<br>
				8. 表格垂直靠下(5) ---> 5<br>
				9. 删除行(15) ---> 15<br>
			</div>
 	</div>
 	<div style="overflow:hidden;clear:both;"></div>
 	<div style="margin:10px;background-color:#aaa"><h4>Excel</h4></div>
 	<div style="margin:20px;"><div style="width:15px;float:left;">3.&nbsp;&nbsp;</div>
 	<div style="margin-bottom:5px;margin-left:30px;"><br>
		1、区域A1:D1的格式设置为:黑体,16号,加粗,倾斜,然后合并居中<br>
		<br>
		2、设置第2行行高为:30,设置B-D列列宽为:25<br>
		<br>
		3、将A2:D2设置为:绿色填充(标准色)<br>
		<br>
		4、将A2:D11内、外边框设置为:蓝色(标准色)<br>
		<br>
		5、用公式计算8月城乡环比涨幅差距,将结果填写在B15<br>
		   8月城乡环比涨幅差距=农村环比涨跌幅-城市环比涨跌幅<br>
		<br>
	</div>
	<div style="background-color:#eeee77;margin-left:40px;">得分:20 </div>
	<div style="background-color:#eeeeee;margin-left:40px;">
		解析:<br>0. 字体(5) ---> 5<br>
		1. 字号(5) ---> 5<br>
		2. 加粗(5) ---> 5<br>
		3. 斜体(5) ---> 5<br>
		4. 合并单元格(5) ---> 5<br>
		5. 合并居中(5) ---> 5<br>
		6. 行高(10) ---> 10<br>
		7. 列宽(10) ---> 10<br>
		8. 填充色(10) ---> 10<br>
		9. 外框横线颜色(5) ---> 5<br>
		10. 外框竖线颜色(5) ---> 5<br>
		11. 内框横线颜色(5) ---> 5<br>
		12. 内框竖线颜色(5) ---> 5<br>
		13. 公式(20) ---> 20<br>
	</div>
 </div>
 	<div style="overflow:hidden;clear:both;"></div>
 	<div style="margin:10px;background-color:#aaa"><h4>PowerPoint操作题</h4></div>
 	<div style="margin:20px;"><div style="width:15px;float:left;">4.&nbsp;&nbsp;</div>
	<div style="margin-bottom:5px;margin-left:30px;"><br>
		1、将第1张幻灯片的切换效果设置为:随机线条 <br>
		   效果选项:水平 <br>
		   自动换片时间:5秒 <br>
		<br>
		2、第3张幻灯片上的图片设置动画效果为:进入——缩放 <br>
		   开始:与上一动画同时 <br>
		   声音:鼓声(drumroll.wav) <br>
		<br>
		3、第3张幻灯片上文本框的字体设置为:Webdings<br>
		<br>
		4、在第5张幻灯片上插入任意样式艺术字:谢谢学习 <br>
		   文字格式:楷体,倾斜,60号字 <br>
		<br>
	</div>
	<div style="background-color:#eeee77;margin-left:40px;">得分:20 </div>
	<div style="background-color:#eeeeee;margin-left:40px;">
		解析:<br>0. 切换的效果(10) ---> 10<br>
		1. 换页方式(5) ---> 5<br>
		2. 换片时间(5) ---> 5<br>
		3. 动画类型(10) ---> 10<br>
		4. 动画开始方式(10) ---> 10<br>
		5. 动画的声音(10) ---> 10<br>
		6. 输入谢谢学习(10) ---> 10<br>
		7. 字体(10) ---> 10<br>
		8. 倾斜(10) ---> 10<br>
		9. 字号(10) ---> 10<br>
		10. 设置Webdings字体(10) ---> 10<br>
	 </div>
 </div>
 	<div style="overflow:hidden;clear:both;"></div>
 	<div style="margin:10px;background-color:#aaa"><h4>flash操作题</h4></div>
 	<div style="margin:20px;">
 		<div style="width:15px;float:left;">5.&nbsp;&nbsp;</div>
		<div style="margin-bottom:5px;margin-left:30px;"><br>
			1、设置舞台高度为:250,背景为:#66CCFF<br>
			<br>
			2、在纸飞机图层上方添加引导层,命名为:path(图层不锁定)<br>
			<br>
			3、使用铅笔工具在path图层第1帧,绘制一条红色(#FF0000)引导线<br>
			<br>
			4、纸飞机图层第40帧插入关键帧,第1帧创建:运动动画,动画属性:调整到路径<br>
			<br>
		</div>
		<div style="background-color:#eeee77;margin-left:40px;">得分:20 </div>
		<div style="background-color:#eeeeee;margin-left:40px;">
			解析:<br>0. 舞台高度(10) ---> 10<br>
			1. 舞台背景(10) ---> 10<br>
			2. path图层名(5) ---> 5<br>
			3. path图层位置(5) ---> 5<br>
			4. path是引导层(10) ---> 10<br>
			5. 纸飞机是被引导层(10) ---> 10<br>
			6. path第1帧绘制引导线(10) ---> 10<br>
			7. 纸飞机第1帧创建运动动画(10) ---> 10<br>
			8. 调整到路径(10) ---> 10<br>
			9. 40帧插入关键帧(10) ---> 10<br>
			10. 引导线是红色的(10) ---> 10<br>
		</div>
 	</div>
	<div style="overflow:hidden;clear:both;"></div>
 </body>
 </html>

这里注意f = open(‘文件名3.csv’,‘w’,newline=‘’,encoding=‘utf-8’) 表示csv文件以utf-8编码保存,但是用excel打开时会出现中文乱码,如不想出现可以可以换成gb18030就可以显示中文了,但是使用记事本打开时乱码,原因是编码不是utf-8

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值