10
12

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 5 years have passed since last update.

Selenium でスクレイピング [Python]

Last updated at Posted at 2015-11-03

PythonにてSeleniumを用い、スクレイピングを行う。

0. 環境

MacOSX10.11.1

CentOSの場合

Xvfb その他諸々が必要
参考: CentOS6.4 + Selenium の環境作成

1. pipでSeleniumをインストール


$ sudo pip install selenium

CentOSの場合

別途、ライブラリのインストールが必要。


$ sudo pip install pyvirtualdisplay

2. 実行

2.1. Seleniumをインポート

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# pyvirtualdisplayを用いる場合
from pyvirtualdisplay import Display
display = Display(visible=0, size=(1024, 768))
display.start()

2.2. ブラウザ(FireFox)を開く


driver = webdriver.Firefox()

後からLinux(CentOS)でスクリプトを回す際に、
FireFoxの方が便利(楽)なのでFireFoxを使う。

2.3. URLを開く

url = "http://google.com"
driver.get(url)

2.4. ブラウザを閉じる

driver.close()

# pyvirtualdisplayを用いる場合
display.stop()
10
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
10
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?