Majoris
Majoris

Reputation: 3189

Extract specific data from html parsing using python

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>')

Upvotes: 0

Views: 152

Answers (2)

dabingsou
dabingsou

Reputation: 2469

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

Upvotes: 1

Niranjan Kumar
Niranjan Kumar

Reputation: 1518

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

Upvotes: 2

Related Questions