0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Python】BeautifulSoupでタグを指定して削除

Last updated at Posted at 2020-07-31

最近、Pythonでスクレイピングをしていて、いらないタグを指定して削除したいな〜〜〜〜と思ったらあったのでメモ

BeautifulSoupインストール

BeautifulSoupを使用するので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)

# 出力結果
# <a href="https://www.sample.com/">Now <br/>available!</a>
# <None></None>

参考
https://www.whyit.work/entry/2019/04/04/101538
https://qiita.com/mtskhs/items/edf7dbba9b0b0246ef8f

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?