[Python] Delete by specifying a tag with Beautiful Soup

Recently, I was scraping with Python and I wanted to specify unnecessary tags and delete them ~ ~ ~ ~, so make a note

Beautiful Soup installation

I will use BeautifulSoup, so install BeautifulSoup4.

$ pip install beautifulsoup4

sample

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

[Python] Delete by specifying a tag with Beautiful Soup
Get the link destination URL by specifying a text sentence with Python scraping (Beautiful Soup) + XPath
Try scraping with Python + Beautiful Soup
[Python] A memorandum of beautiful soup4
[Python] Scraping a table using Beautiful Soup
Stock number ranking by Qiita tag with python
Read line by line from a file with Python
My Beautiful Soup (Python)
Scraping with Beautiful Soup
[Python] Draw a Qiita tag relationship diagram with NetworkX
I learned Python with a beautiful girl at Paiza # 02
I learned Python with a beautiful girl at Paiza # 01
JPEG image generation by specifying quality with Python + OpenCV
Make a fortune with Python
Create a directory with python
Table scraping with Beautiful Soup
Crawl practice with Beautiful Soup
Library for specifying a name server and dig with python
[Python] What is a with statement?
A memorandum when using beautiful soup
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
Scraping multiple pages with Beautiful Soup
Let's make a GUI with python.
Solve ABC166 A ~ D with Python
Create a virtual environment with Python!
I made a fortune with Python.
[Python] A memo to operate ROM created by GBDK with PyBoy
Draw a beautiful circle with numpy
Scraping pages with pagination with Beautiful Soup
Scraping with Beautiful Soup in 10 minutes
Building a virtual environment with Python 3
Solve ABC168 A ~ C with Python
Make a recommender system with python
I want to send barometric pressure information (obtained by Beautiful Soup) with a graph to Slack with Python (+ I want to manage it with Google Spread Sheet)
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Let's make a graph with python! !!
Pass a list by reference from Python to C ++ with pybind11
[Python] Inherit a class with class variables
I made a daemon with Python
Website scraping with Python's Beautiful Soup
Write a batch script with Python3.5 ~
[Python] Get elements by specifying attributes with prefix search in BeautifulSoup
I tried to communicate with a remote server by Socket communication with Python.
Download the file by specifying the download destination with Python & Selemiun & Chrome (Windows version)
Settings when using Python 3 requests and Beautiful Soup with crostini on Chromebook
Sort anime faces by scraping anime character pages with Beautiful Soup and Selenium
[Ev3dev] Let's make a remote control program by Python with RPyC protocol
How to sort by specifying a column in the Python Numpy array.
[Pyenv] Building a python environment with ubuntu 16.04
Get property information by scraping with python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Create a Python function decorator with Class
[Python] A program that creates stairs with #
Building a Python3 environment with Amazon Linux2
Let's make a shiritori game with Python
Install Python as a Framework with pyenv
Build a blockchain with Python ① Create a class
[Python3] Understand the basics of Beautiful Soup