How do I further scrape the string from an extracted element in html using python?

Viewed 27
counter=0
for col in soup.find(class_='m-nktq8b'):
    if counter==0:
        counter+=1
        continue
    else:
        print(col)
        print(col['class'])

Above is my input, and below is my output:

<p class="m-1nj5h5j">Severe</p>

['m-1nj5h5j']

How could I further scrape the word between > and <, which is 'Severe'?

1 Answers

You can extract this using regex:

import re

text = '<p class="m-1nj5h5j">Severe</p>'

print(re.search(r'<p .+?>(.+)</p>', text).group(1))

Output is:

Severe

However, it looks more like a work-around than a solution. A better solution is to extract this using just beautifulsoup's find:

counter = 0
for col in soup.find(class_="m-nktq8b"):
    if counter == 0:
        counter += 1
        continue
    else:
        # here is the line:
        print(col.find("p"))

Checkout this question for more information.

Related