LoginSignup
15
12

More than 5 years have passed since last update.

PythonのBeautifulSoupで取得した要素(タグ)の属性値を抽出

Last updated at Posted at 2018-06-19

ざっくりと説明

BeautifulSoupを使用していてある得意のinputタグのvalueを抽出する場面が合ったので、備忘録として掲載。

xxx.html
<html>
    <head>
    ・・・
    </head>
    <body>
        選択してください。
         ・・・  
           <input id="data" value="value01">
         ・・・
    <body>
</html>
xxx.py
import logging
import requests
from bs4 import BeautifulSoup

url = 'http://hoge/xxx.html'
res = requests.get(url)
soup = BeautifulSoup(res.content, 'html5lib')

# ID「data」を持っているinputタグを抽出
js = soup.find('input',id='data')
# valueの値を抽出
jsonData = js['value']
logging.info(jsonData)

参考サイト

取得した要素(タグ)の属性にアクセスする - Rails etc... Memo

15
12
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
15
12