G
G
GH0st_is_HAcker2021-08-29 17:22:30
Parsing
GH0st_is_HAcker, 2021-08-29 17:22:30

How to remove unwanted tag and its content using Beatifulsoup?

At the input I have the following code:

<div class="col-xs-6 col">
   <strong>Views</strong>
   "222"
</div>

When I wrote this code:
r = requests.get(url=url, headers=headers)
soup = Beautifulsoup(r, "lxml")
Views = soup.find("div", class_="col-xs-6-col").text
I get the output:
views

222

And I need the output to be Toko 222

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2021-08-29
@phaggi

You can remove lines with the “strong” tag from the text of the page something like this:

data = '''<div class="col-xs-6 col">
   <strong>Views</strong>
   "222"
</div>
'''

print(*[''.join([line, '\n']) for line in data.split('\n') if not 'strong>' in line])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question