Recently, I was scraping with Python and I wanted to specify unnecessary tags and delete them ~ ~ ~ ~, so make a note
I will use BeautifulSoup, so install BeautifulSoup4.
$ pip install beautifulsoup4
from bs4 import BeautifulSoup
>>> marks = '<p><span class="category">Information</span><span class="bdy"><a href="https://www.sample.com/">Now <br>available!</a></span></p>'
>>> soup = BeautifulSoup(marks, 'html.parser')
>>> a_tag = soup.find("a")
>>> print(a_tag)
>>> br_tag = soup.find("a")
>>> br_tag.decompose()
>>> print(a_tag)
#Output result
# <a href="https://www.sample.com/">Now <br/>available!</a>
# <None></None>
reference https://www.whyit.work/entry/2019/04/04/101538 https://qiita.com/mtskhs/items/edf7dbba9b0b0246ef8f
Recommended Posts