首頁 > 軟體

Python如何提取html中文字到txt

2023-11-04 06:01:14

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('[ trn]+', ' ', text) 
            self.__text.append(text + ' ') 
 
    def handle_starttag(self, tag, attrs): 
        if tag == 'p': 
            self.__text.append('nn') 
        elif tag == 'br': 
            self.__text.append('n') 
 
    def handle_startendtag(self, tag, attrs): 
        if tag == 'br': 
            self.__text.append('nn') 
 
    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 [""20122074[email protected]'
##mtch = reg.match(msg)
##print(mtch.group(1))

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com