10
8

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で属性を前方一致検索で指定して要素を取得する

Posted at

スクレイピングする際に属性の値が動的になっている要素を取得する際に少しハマったので共有。

#HTMLサンプル

.html
<div class="hogehoge">
    <input type="hidden" id="sample_1" value="fugafuga">
    <input type="hidden" id="sample_2" value="fugafuga">
    <input type="hidden" id="sample_3" value="fugafuga">
    <input type="hidden" id="sample_4" value="fugafuga">
    <input type="hidden" id="sample_5" value="fugafuga">
    ・
    ・
    ・
</div>

このような形で要素数が不定で属性の値が動的になっている要素を取得したいとする。

#取得方法
・lambda関数ver

.py
source = soup.find_all('input', id=lambda value: value and value.startswith('sample_'))

lambda関数を使用して前方一致検索で要素を取得出来る。

・正規表現ver

.py
source = soup.find_all('input', id=re.compile('^sample_'))

正規表現で取得も可能。

・CSSセレクタver

.py
source = soup.select('input[id^=sample_]')

CSSセレクタで指定する事も可能。

#参考リンク
How to find all divs who's class starts with a string in BeautifulSoup?

10
8
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
10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?