Skip to content

Instantly share code, notes, and snippets.

@GuyMicciche
Created August 17, 2023 20:55
Show Gist options
  • Save GuyMicciche/5eee49505d1c386824e147843b083a4f to your computer and use it in GitHub Desktop.
Save GuyMicciche/5eee49505d1c386824e147843b083a4f to your computer and use it in GitHub Desktop.
Extract the content of the second themeScrp tag from the list of all themeScrp contents
from html.parser import HTMLParser
class MyHTMLParser(HTMLParser):
def __init__(self):
super().__init__()
self.recording = False
self.current_data = []
self.all_themeScrp_contents = []
def handle_starttag(self, tag, attrs):
if tag == 'p':
for name, value in attrs:
if name == 'class' and 'themeScrp' in value.split():
self.recording = True
self.current_data = [] # Clear current data for new themeScrp tag
break
def handle_endtag(self, tag):
if self.recording and tag == 'p':
self.recording = False
self.all_themeScrp_contents.append(''.join(self.current_data).strip())
def handle_data(self, data):
if self.recording:
self.current_data.append(data)
parser = MyHTMLParser()
parser.feed(inputData['htmlContent'])
# Extract the content of the second themeScrp tag from the list of all themeScrp contents
extractedText = parser.all_themeScrp_contents[1] if len(parser.all_themeScrp_contents) > 1 else 'Not Found'
return {'extractedText': extractedText}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment