Pythonのseleniumでsrc属性を取得する方法
PythonのSeleniumを使用して<img>タグや他の要素のsrc属性を取得するには、get_attributeメソッドを使用します。以下は、Seleniumを使用して<img>タグのsrc属性を取得する方法の例です。
python
from selenium import webdriver
# WebDriverのインスタンスを作成(例:Chrome用のWebDriver)
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
# ウェブページを開く
driver.get('https://example.com')
# <img>タグを取得
img_elements = driver.find_elements_by_tag_name('img')
# 各<img>タグのsrc属性を取得して表示
for img_element in img_elements:
src_attribute = img_element.get_attribute('src')
print("画像のURL(src属性):", src_attribute)
# WebDriverを終了
driver.quit()
このコードでは、Seleniumを使用してWebDriverのインスタンスを作成し、指定したURLのウェブページを開きます。それからfind_elements_by_tag_nameメソッドを使用して、すべての<img>タグ(画像要素)を取得します。各<img>タグに対してget_attributeメソッドを使用してsrc属性の値を取得し、それを表示します。
この方法を使用して、Seleniumを使ってウェブページから画像のURLを取得できます。必要な処理を行う際にこれらのURLを活用できます。同様の方法を他の要素のsrc属性に適用することもできます。

ディスカッション
コメント一覧
まだ、コメントがありません