LoginSignup
10
7

More than 3 years have passed since last update.

[Python]BeautifulSoupで属性を前方一致検索で指定して要素を取得する

Posted at

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

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

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

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

・正規表現ver

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

正規表現で取得も可能。

・CSSセレクタver

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

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

参考リンク

How to find all divs who's class starts with a string in BeautifulSoup?

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