2015-08-21 4 views
5

'साफ' के लिए मैं निम्नलिखित पाठ है:सबसे अच्छा तरीका है एचटीएमएल पाठ

"It's the show your only friend and pastor have been talking about! 
<i>Wonder Showzen</i> is a hilarious glimpse into the black 
heart of childhood innocence! Get ready as the complete first season of MTV2's<i> Wonder Showzen</i> tackles valuable life lessons like birth, 
nature, diversity, and history &#8211; all inside the prison of 
your mind! Where else can you..." 

क्या मैं इस के साथ क्या करना चाहते हैं HTML टैग निकालें और यूनिकोड में यह सांकेतिक शब्दों में बदलना है। मैं वर्तमान में कर रहा हूँ:

def remove_tags(text): 
    return TAG_RE.sub('', text) 

कौन सा केवल टैग स्ट्रिप्स। मैं डाटाबेस स्टोरेज के लिए ऊपर से सही तरीके से एन्कोड कैसे करूं?

+0

इस विषय की जांच करें http://stackoverflow.com/questions/23380171/using-beautifulsoup-extract-text-without-tags –

+0

क्या आप कृपया समझा सकते हैं, जब आप इसे यूनिकोड में एन्कोड करते हैं, तो आप आउटपुट के रूप में क्या उम्मीद कर रहे हैं? –

+0

वैसे, आप अपने regexp के साथ क्या कर रहे हैं गलत है। ऐसा मत करो। HTML को regexp का उपयोग करके पार्स नहीं किया जा सकता है, इसलिए ऐसा करने के सभी प्रयास विफल होने के लिए बाध्य हैं। इसके बजाय एक HTML पार्सर का उपयोग करें, यही वह है जो वे हैं। – spectras

उत्तर

2

आप एक HTML पार्सर के माध्यम से अपना टेक्स्ट पास करने का प्रयास कर सकते हैं। यहाँ BeautifulSoup का उपयोग कर एक उदाहरण है:

from bs4 import BeautifulSoup 

text = '''It's the show your only friend and pastor have been talking about! 
<i>Wonder Showzen</i> is a hilarious glimpse into the black 
heart of childhood innocence! Get ready as the complete first season of MTV2's<i> Wonder Showzen</i> tackles valuable life lessons like birth, 
nature, diversity, and history &#8211; all inside the prison of 
your mind! Where else can you...''' 

soup = BeautifulSoup(text) 

>>> soup.text 
u"It's the show your only friend and pastor have been talking about! \nWonder Showzen is a hilarious glimpse into the black \nheart of childhood innocence! Get ready as the complete first season of MTV2's Wonder Showzen tackles valuable life lessons like birth, \nnature, diversity, and history \u2013 all inside the prison of \nyour mind! Where else can you..." 

अब आप HTML निकाय के साथ एक यूनिकोड स्ट्रिंग परिवर्तित यूनिकोड वर्ण से बच गया है, अर्थात &#8211;\u2013 में बदल दिया गया।

यह HTML टैग को भी हटा देता है।

+0

यहां: http: //stackoverflow.com/questions/275174/how-do-i-perform-html-decoding-encoding-using-python-django – dsgdfg

संबंधित मुद्दे