2
2

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.

Firefox の Headless モード (python3)

Last updated at Posted at 2018-08-12

Firefox の Headless モードの使い方です。
次のページを参考にしました。
Headless mode

Arch Linux で確認しました。

必要なソフトのインストール

sudo pacman -S firefox
sudo pacman -S python-selenium
sudo pacman -S geckodriver


```py:headless_firefox.py
#! /usr/bin/python
# ------------------------------------------------------------------
#
#	headless_firefox.py
#
#						Nov/12/2018
# ------------------------------------------------------------------
import	sys
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWait

## from selenium import webdriver
from selenium.webdriver.firefox.options import Options

sys.stderr.write("*** 開始 ***\n")
url_target = sys.argv[1]
sys.stderr.write("url_target = " + url_target + "\n")
#
options = Options()
options.add_argument('-headless')
driver = Firefox(executable_path='/usr/bin/geckodriver', options=options)
wait = WebDriverWait(driver, timeout=10)
driver.get(url_target)

driver.save_screenshot("out.png")
html = driver.page_source

driver.quit()

print(html)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行結果

$ ./headless_firefox.py  https://ekzemplaro.org/storytelling/
*** 開始 ***
url_target = https://ekzemplaro.org/storytelling/
<html lang="ja"><head>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8">
<script src="/js/jquery-3.3.1.min.js"></script>
<script src="storytelling.js"></script>
<link rel="stylesheet" href="storytelling.css">
<title>ストーリーテリング</title>
</head>
<body>
<h2>私のレパートリー</h2><p>
</p><blockquote>
<div class="contents"><table><tbody><tr><th>no</th><th>title</th><th>text</th><th>khm</th><th>pages</th><th>told</th></tr>
<tr><td>s001</td><td>ブレーメンの音楽隊</td><td>子どもに語る グリムの昔話 4</td><td>27</td><td>10</td><td>2018-5-31</td></tr>
<tr><td>s002</td><td>ラプンツェル</td><td>子どもに語る グリムの昔話 3</td><td>12</td><td>12</td><td>2016-9-14</td></tr>

略

</div>
</blockquote>
<hr>
<a href="html_src/">src</a><br>
<hr>
<a href="../">Return</a><p>
Aug/04/2018 AM 07:00</p><p>


</p></body></html>
*** 終了 ***

Javascript が実行されない時は、timeout=10 を timeout=20 にすれば実行されます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?