網站首頁 編程語言 正文
Python提取html中文本到txt
正則去標簽方式
# -*- coding: utf-8 -*-
import re
def html_tag_rm(content: str):
dr = re.compile(r'<[^>]+>',re.S)
return dr.sub('',content)
nltk
比較笨重
需要安裝依賴 nltk, numpy, pyyaml
# -*- coding: utf-8 -*-
import nltk
def html_tag_rm(content: str):
return nltk.clean_html(content)
htmlParser
import re
from sys import stderr
from traceback import print_exc
from HTMLParser import HTMLParser
class _DeHTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.__text = []
def handle_data(self, data):
text = data.strip()
if len(text) > 0:
text = re.sub('[ \t\r\n]+', ' ', text)
self.__text.append(text + ' ')
def handle_starttag(self, tag, attrs):
if tag == 'p':
self.__text.append('\n\n')
elif tag == 'br':
self.__text.append('\n')
def handle_startendtag(self, tag, attrs):
if tag == 'br':
self.__text.append('\n\n')
def text(self):
return ''.join(self.__text).strip()
def dehtml(text):
try:
parser = _DeHTMLParser()
parser.feed(text)
parser.close()
return parser.text()
except:
print_exc(file=stderr)
return text
def main():
text = r'''''
<html>
<body>
<b>Project:</b> DeHTML<br>
<b>Description</b>:<br>
This small script is intended to allow conversion from HTML markup to
plain text.
</body>
</html>
'''
print(dehtml(text))
if __name__ == '__main__':
main()
Python提取txt正則內容
其中:
pattern = re.compile(r'^.["“subject”"] [([^[])].*')
為修改的正則匹配部分
import re
import pandas as pd
with open("C:/data1.txt", 'r', encoding='UTF-8') as f:
data = f.readlines()
f.close()
tol = []
for line in data:
##s = re.findall('[\u4e00-\u9fa5]', data) print(s)
pattern = re.compile(r'^.*\[\"\"subject\"\"\] \[([^\[]*)\].*')
string = str(line)
url = re.findall(pattern,string)
if (url is not None ) and (url != '[]'):
tol.append(url)
print(tol)
pd.DataFrame(tol).to_csv('C:/tol2.csv')
##f1 = open("url.txt", "a+", encoding='utf-8')
##for urls in url:
## f1.write(urls + '\n')
##f1.close()
##reg = re.compile(r'^.*\[\"\"subject\"\"\] \[(.*)\]')
##msg = '""i;octet"" [""subject""] [""小木蟲""] ,accounts :in_main [""2012207469@tju.edu.c'
##mtch = reg.match(msg)
##print(mtch.group(1))
總結
原文鏈接:https://blog.csdn.net/chichu261/article/details/107526195
相關推薦
- 2022-07-29 Django?狀態保持搭配與存儲的實現_python
- 2023-03-18 C#調用dll報錯:無法加載dll,找不到指定模塊的解決_C#教程
- 2022-12-26 Golang判斷struct/slice/map是否相等以及對比的方法總結_Golang
- 2022-11-21 詳解React獲取DOM和獲取組件實例的方式_React
- 2022-11-13 詳解Redis?Stream做消息隊列_Redis
- 2023-10-16 微信小程序radio單選按鈕選中與取消
- 2022-07-13 Sybase使用Spring的只讀事物報 Use ‘set readonly off‘ to exe
- 2023-04-13 微信分享,wx.config參數設置
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支