LoginSignup
22
17

More than 5 years have passed since last update.

selenium+pythonを使ってテキストのリンクURLを取得する

Last updated at Posted at 2019-02-14

取得したいテキストのリンクはこんな感じだとする。

<p id="id_hoge"><a href="https://hogehoge.html" target="_blank">hoogehoge</a></p>

コードはこんな感じ。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.binary_location = "Chromeのパス"
driver = webdriver.Chrome(chrome_options=options, executable_path="ChromeDriverのexeのパス")
driver.get('取得したいページのURL')
element = driver.find_element_by_id('id_hoge')
aTag    = element.find_element_by_tag_name("a")
url     = aTag.get_attribute("href")

変数urlには見事に
https://hogehoge.html
という値が入る。

22
17
1

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
22
17