Removing <!--//--><![CDATA[//> from HTML

user2085779

I am trying to remove [CDATA[ from HTML file but it is not working

This is the code I got

from bs4 import BeautifulSoup

def stripIt(string, tag):
    soup = BeautifulSoup(string)
    data = open("tet.txt",'a')
    rmtags = soup.findAll(tag)
    for t in rmtags:
        string = string.replace(str(t), '')
        data.write(string)
    return string





testhtml = '''

<!--//--><![CDATA[//><!--
jQuery.extend(Drupal.settings, { "basePath": "/", "fivestar": { "titleUser": "Your rating: ", "titleAverage": "Average: ", "feedbackSavingVote": "Saving your vote...", "feedbackVoteSaved": "Your vote has been saved.", "feedbackDeletingVote": "Deleting your vote...", "feedbackVoteDeleted": "Your vote has been deleted." }, "disqus": { "url": "http://beta.mangalam.com/wayanad/139068", "title": "ക്‌നാനായ കത്തോലിക്കാ കോണ്‍ഗ്രസ്സ്‌ കുടുംബസംഗമം നടത്തി", "identifier": "node/139068", "shortname": "mangalam" } });
//--><!]]>


അവഹേളനപരവും വ്യക്തിപരമായ അധിക്ഷേപങ്ങളും അശ്‌ളീല പദപ്രയോഗങ്ങളും ദയവായി ഒഴിവാക്കുക. അഭിപ്രായങ്ങള്‍ മലയാളത്തിലോ ഇംഗ്ലീഷിലോ എഴുതുക. മംഗ്ലീഷില്‍ എഴുതുന്ന അഭിപ്രായങ്ങള്‍ പോസ്റ്റ് ചെയ്യുന്നതല്ല. 
ഇംഗ്ലീഷില്‍ ടൈപ് ചെയ്ത് മലയാളമാക്കാനുള്ള സംവിധാനം ഏര്‍പ്പെടുത്തിയിട്ടുണ്ട്  ടൈപ്പ് ചെയ്യാന്‍ ഇവിടെ ക്ലിക്ക് ചെയ്യുകബൈക്കില്‍ കാട്ടുപന്നിയിടിച്ച്‌ മറിഞ്ഞു: അച്‌ഛനും മകനും പരുക്ക്‌പുല്‍പ്പള്ളി: ഓടിക്കൊണ്ടിരുന്ന ബൈക്കിലേക്ക്‌ കാട്ടുപന്നി... കര്‍ഷക വഞ്ചനക്ക്‌ തെരഞ്ഞെടുപ്പില്‍ തിരിച്ചടി നല്‍കാന്‍ ഹരിതസേന തയാറെടുക്കുന്നുമാനന്തവാടി: ആസന്നമായ പൊതുതെരഞ്ഞെടുപ്പ്‌ ലക്ഷ്യമാക്കി കര്‍ഷക ക്ഷേമ... കോളനി വികസനത്തിന്‌ രണ്ട്‌ കോടിയുടെ ഭരണാനുമതികല്‍പ്പറ്റ: പടിഞ്ഞാറത്തറ പഞ്ചായത്തിലെ പാറനിരപ്പുകുന്ന്‌, ചെമ്പകമൂല... ടെക്‌ഫെസ്‌റ്റ്: മത്സര വിജയികള്‍ക്ക്‌ കാല്‍ലക്ഷത്തിലേറെ തുക സമ്മാനംകല്‍പ്പറ്റ: നൂതനങ്ങളായ സാങ്കേതിക ഉല്‍പന്നങ്ങളെയും പദ്ധതികളേയും... ബത്തേരി സമ്പൂര്‍ണ പെന്‍ഷന്‍ പഞ്ചായത്തായിബത്തേരി: സമ്പൂര്‍ണ പെന്‍ഷന്‍ പദ്ധതി നയത്തിന്റെ ഭാഗമായി ബത്തേരി... ബത്തേരി ഉപജില്ലാ കലോത്സവം: മീനങ്ങാടി ജി.എച്ച്‌.എസ്‌. ചാമ്പ്യന്‍മാര്‍പുല്‍പ്പള്ളി: വിജയ എച്ച്‌.എസ്‌.എസില്‍ നടന്ന 54 -ാമത്‌ ബത്തേരി... പുകയില ഉപയോഗത്തിനെതിരേ സൈക്കിള്‍ യാത്രകല്‍പ്പറ്റ: പുകയില ഉത്‌പന്നങ്ങളുടെ ഉപയോഗം ഉപേക്ഷിക്കണമെന്ന... അഞ്ചു വര്‍ഷത്തിനിടെ വയനാടിന്‌ നഷ്‌ടമായത്‌ അഞ്ചര കോടി രൂപകല്‍പ്പറ്റ: സര്‍ക്കാര്‍ സ്‌കൂളുകളില്‍ ചിത്രകലാ അധ്യാപകരുടെയും... പാരലല്‍ കോളേജ്‌ ജില്ലാ കായികമേള: ബത്തേരി മലബാര്‍ കോളേജിന്‌ ഒന്നാം സ്‌ഥാനംകല്‍പ്പറ്റ: എസ്‌.കെ.എം.ജെ. ഹയര്‍സെക്കന്‍ഡറി സ്‌കൂള്‍ ഗ്രൗണ്ടില്‍... ക്രിസ്‌മസ്‌, പുതുവത്സര ആഘോഷങ്ങള്‍ മദ്യവിമുക്‌തമാക്കണംമാനന്തവാടി: ക്രിസ്‌മസ്‌, പുതുവത്സര ആഘോഷങ്ങള്‍ മദ്യവിമുക്‌... ക്രിസ്‌തുമസ്‌-പുതുവത്സരാഘോഷം: വ്യാജമദ്യ ഉല്‍പ്പാദനം തടയുന്നതിന്‌ കണ്‍ട്രോള്‍ റൂംകല്‍പ്പറ്റ: ക്രിസ്‌തുമസ്‌-പുതുവത്സര ആഘോഷത്തോടനുബന്ധിച്ച്‌... സ്‌റ്റേഡിയം ഉദ്‌ഘാടനം: റോഡ്‌ഷോ നടത്തികല്‍പ്പറ്റ:  ഈ മാസം 17-ാം തീയതി ഉദ്‌ഘാടനം ചെയ്യപ്പെടുന്ന കൃഷ്‌... <ul class="news_list">
             <li> <a href="#" target="_blank">സെന്‍സെക്‌സ് 200 പോയന്റ് ഇടിഞ്ഞു</a></li>
    <li> <a href="#">പാകിസ്താനില്‍ മിസൈല്‍ ആക്രമണം: 16 മരണം</a></li>
     <li> <a href="#">സച്ചിന്‍ രാജ്യസഭാംഗമായി സത്യപ്രതിജ്ഞ ചെയ്തു</a></li>
      <li> <a href="#" target="_blank">സെന്‍സെക്‌സ് 200 പോയന്റ് ഇടിഞ്ഞു</a></li>
    <li> <a href="#">പാകിസ്താനില്‍ മിസൈല്‍ ആക്രമണം: 16 മരണം</a></li>
     <li> <a href="#">സച്ചിന്‍ രാജ്യസഭാംഗമായി സത്യപ്രതിജ്ഞ ചെയ്തു</a></li>



'''
#print  stripIt(testhtml, 'li')

How to fix it?

rabs

This code uses a regular expression to find [CDATA[ - and can be customised to remove any string you would like to remove.

import re
string = '<!--//--><![CDATA[//><!--jQuery.extend(Drupal.settings, { "basePath": "/", "fivestar": { "titleUser": "Your rating: ", "titleAverage": "Average: ", "feedbackSavingVote": "Saving your vote...", "feedbackVoteSaved": "Your vote has been saved.", "feedbackDeletingVote": "Deleting your vote...", "feedbackVoteDeleted": "Your vote has been deleted." }, "disqus": { "url": "http://beta.mangalam.com/wayanad/139068", "title": "???????? ???????????? ?????????????? ??????????? ??????", "identifier": "node/139068", "shortname": "mangalam" } });//--><!]]>'
first = re.search('\[CDATA\[', string)
string = string[:first.span()[0]]+string[first.span()[1]:]

If there are multiple versions of [CDATA[ (or any other string) to remove, you could use the following:

import re
string = '<!--//--><![CDATA[//><!--jQuery.extend(Drupal.settings, { "basePath": "/", "fivestar": { "titleUser": "Your rating: ", "titleAverage": "Average: ", "feedbackSavingVote": "Saving your vote...", "feedbackVoteSaved": "Your vote has been saved.", "feedbackDeletingVote": "Deleting your vote...", "feedbackVoteDeleted": "Your vote has been deleted." }, "disqus": { "url": "http://beta.mangalam.com/wayanad/139068", "title": "???????? ???????????? ?????????????? ??????????? ??????", "identifier": "node/139068", "shortname": "mangalam" } });//--><!]]>, [CDATA['
first = re.finditer('\[CDATA\[', string)
for element in first:
    print element.span()
    string = string[:element.span()[0]]+string[element.span()[1]:]
print string

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related