0

How to extract only a few select tags' data in Python parsing an HTML file? I am looking to get the data for just the first two h2 tags.

from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print("Encountered a start tag:", tag)

    def handle_endtag(self, tag):
        print("Encountered an end tag :", tag)

    def handle_data(self, data):
        print("Encountered some data  :", data)

parser = MyHTMLParser()
parser.feed('<html><head><title>Test</title></head>'
            '<body><h1>Parse me!</h1><h2>firstname</h2><h2>lastname</2><p>next text</p><h2>something else<h2></body></html>')

2 Answers 2

2

You can use beautifulsoup4 for this purpose

pip install beautifulsoup4

from bs4 import BeautifulSoup
html = '''<html><head><title>Test</title></head><body><h1>Parse me!</h1><h2>firstname</h2><h2>lastname</h2><p>next text</p><h2>something else<h2></body></html>'''
soup = BeautifulSoup(html)
tag_list = soup.findAll('h2') # Specify the tag
print(tag_list[0].string) # Fetches tag data on the basis of index
print(tag_list[1].string) # Fetches tag data on the basis of index

Output:

firstname
lastname
Sign up to request clarification or add additional context in comments.

Comments

1

There's also a solution here.

from simplified_scrapy import SimplifiedDoc
html = '''<html><head><title>Test</title></head><body><h1>Parse me!</h1><h2>firstname</h2><h2>lastname</h2><p>next text</p><h2>something else<h2></body></html>'''
doc = SimplifiedDoc(html)
h2 = doc.h2
print (h2.text, h2.getNext('h2').text)

h2s = doc.selects('h2>text()')
print (h2s[0], h2s[1])

Result:

firstname lastname
firstname lastname

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.