LoginSignup
10

More than 3 years have passed since last update.

posted at

updated at

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

ざっくりと説明

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

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
What you can do with signing up
10