#coding:utf8

import hashlib
import codecs
from bs4 import BeautifulSoup
import re


def getHtmlText(sourceHtml):
    _soup = BeautifulSoup(sourceHtml,"lxml")
    list_a = _soup.find_all("a")
    for _a in list_a:
        _href = _a.attrs.get("href","")
        if _href.find("www.bidizhaobiao.com")>0:
            _a.decompose()
    # richText = _soup.find("div",attrs={"class":"richTextFetch"})
    # if richText is not None:
    #     richText.decompose()
    _text = _soup.get_text()

    _text = re.sub("\s*",'',_text)
    if len(_text)==0:
        _text = str(_soup)
    return _text

def getMD5(sourceHtml):
    if sourceHtml is not None and len(sourceHtml)>0:
        _text = getHtmlText(sourceHtml)
        if isinstance(_text,str):
            bs = _text.encode()
        elif isinstance(_text,bytes):
            bs = _text
        else:
            return ""
        md5 = hashlib.md5()
        md5.update(bs)
        return md5.hexdigest()
    return ""


def getFingerprint(sourceHtml):
    md5 = getMD5(sourceHtml)
    if md5!="":
        _fingerprint = "md5=%s"%(md5)
    else:
        _fingerprint = ""
    return _fingerprint

if __name__=="__main__":
    # sourceHtml = codecs.open("C:\\Users\\\Administrator\\Desktop\\2.html","rb",encoding="utf8").read()
    sourceHtml = "天全县农村敬老院护理能力提升改造项目初步设计及概算审批公示"+'<div> <div> <div> 天全县农村敬老院护理能力提升改造项目初步设计及概算审批公示 </div> <div> <div> <p>一、办理事项：天全县农村敬老院护理能力提升改造项目初步设计及概算审批</p> <p>二、项目业主：天全县民政局</p> <p>三、项目代码：2107-511825-04-01-642123</p> <p>四、办理状态：办结。</p> <p>五、办理时间：2024年5月14日</p> </div> </div> </div> </div>'
    sourceHtml = "天全县农村敬老院护理能力提升改造项目初步设计及概算审批公示"+'审批项目'
    print(getFingerprint(sourceHtml))