午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    Python轉(zhuǎn)換HTML到Text純文本的方法
    來(lái)源:易賢網(wǎng) 閱讀:2935 次 日期:2015-01-16 14:07:06
    溫馨提示:易賢網(wǎng)小編為您整理了“Python轉(zhuǎn)換HTML到Text純文本的方法”,方便廣大網(wǎng)友查閱!

    本文實(shí)例講述了Python轉(zhuǎn)換HTML到Text純文本的方法。分享給大家供大家參考。具體分析如下:

    今天項(xiàng)目需要將HTML轉(zhuǎn)換為純文本,去網(wǎng)上搜了一下,發(fā)現(xiàn)Python果然是神通廣大,無(wú)所不能,方法是五花八門。

    拿今天親自試的兩個(gè)方法舉例,以方便后人:

    方法一:

    1. 安裝nltk,可以去pipy裝

    (注:需要依賴以下包:numpy, PyYAML)

    2.測(cè)試代碼:

    復(fù)制代碼 代碼如下:>>> import nltk

    >>> aa = 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>

    '''

    >>> aa

    '\n<html>\n <body>\n <b>Project:</b> DeHTML<br>\n <b>Description</b>:<br>\n This small script is intended to allow conversion from HTML markup to \n plain text.\n </body>\n </html>\n '

    >>> <strong>print nltk.clean_html(aa)</strong>

    Project: DeHTML

    Description :

    This small script is intended to allow conversion from HTML markup to

    plain text.

    方法二:

    如果覺(jué)得nltk太笨重,大材小用的話,可以自己寫代碼,代碼如下:

    復(fù)制代碼 代碼如下:from HTMLParser import HTMLParser

    from re import sub

    from sys import stderr

    from traceback import print_exc

    class _DeHTMLParser(HTMLParser):

    def __init__(self):

    HTMLParser.__init__(self)

    self.__text = []

    def handle_data(self, data):

    text = data.strip()

    if len(text) > 0:

    text = 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()

    運(yùn)行結(jié)果:

    >>> ================================ RESTART ================================

    >>>

    Project: DeHTML

    Description :

    This small script is intended to allow conversion from HTML markup to plain text.

    更多信息請(qǐng)查看IT技術(shù)專欄

    更多信息請(qǐng)查看腳本欄目
    易賢網(wǎng)手機(jī)網(wǎng)站地址:Python轉(zhuǎn)換HTML到Text純文本的方法
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國(guó)考·省考課程試聽(tīng)報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)