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.

beautiful soupを使ってみた際の備忘録

Last updated at Posted at 2020-07-18

##使ってみた
pip install beautifulsoup4でインストール
パーサー?なるものはデフォルトでいいかなと思って、lxmlとかは使わずデフォルトで備わっているhtml.parserを用いた。

import requests
from bs4 import BeautifulSoup
url = input()
html = requests.get(url)
soup = BeautifulSoup(html.content, "html.parser")

基本はこれで良いはず。

###検索
・id検索(検索できるものは一つ
soup.find(id="id名")
・css selector検索(検索できるものは一つ
ing.select_one("css selector名")

検索に一致する全ての要素を見つける時は
idなら
find_all(id名)
css selectorなら
select(.class属性名)
参照:[Beautiful Soup のfind_all( ) と select( ) の使い方の違い]
(https://gammasoft.jp/blog/difference-find-and-select-in-beautiful-soup-of-python/)

<h3 class="A B">のような(class属性を複数持っている)物をselectで検索する時はselect_one(.A.B)をする。

###参考にしたサイトのURL
cssセレクタについて
タグ検索とcssセレクタ検索の例が載っている

##追記
###前方一致、後方一致、部分一致(20/09/12)
前方, 後方, 部分一致セレクタ

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?