2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

selenium 備忘録

Posted at

スクレイピングでjavascriptが絡むデータが必要になり、beautifulsoupではできない場合にはseleniumを使う必要がある。

その際のseleniumの使い方。

まず事前にドライバーをダウンロードする必要がある。

コード

#seleniumのimport
from selenium import webdriver
#elementの指定で使う
from selenium.webdriver.common.by import By

#サイトのURL
URL = ""
#ドライバーのパス
PATH = ""

driver = webdriver.Chrome(PATH)
#サイトを開く
driver.get(URL)

getを呼び出すとブラウザが立ち上がる。

elementの取得方法

#クラス名で取得
posts = driver.find_elements_by_class_name("post")
for i in range(len(posts)):
    #この方法でもクラス名からelementを取得できる
    meta = posts[i].find_element(By.CLASS_NAME, value="meta")

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?