「広告」

python3 スクレイピング の初歩 for Centos7

「広告」
記事内に広告が含まれています。
「広告」

python3で、スクレイピングの初級レベルのセットアップを!

「広告」

キャプチャープログラム

# vi test.py
----------------------------------------
#!/usr/bin/env  python3.6
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--window-size=1280,1024')

driver = webdriver.Chrome(options=options)
driver.get('http://www.xxxx.xxx/phpinfo.php')

driver.save_screenshot('test.png')
driver.quit()

↑というプログラムで、あるサイトのキャプチャーをとるプログラムを稼働させます。

python 3.xだけを入れた状態からスタートです。

「広告」

seleniumのエラー

# ./test1.py 
Traceback (most recent call last):
  File "./test1.py", line 2, in <module>
    from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'

↑当然ながら、エラーが出ます。「selenium」が無いと・・

# pip install selenium
Collecting selenium
  Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
    100% |■■■■■■■■■■■■■■| 911kB 822kB/s 
Requirement already satisfied (use --upgrade to upgrade): urllib3 in /usr/lib/python2.7/site-packages (from selenium)
Installing collected packages: selenium
Successfully installed selenium-3.141.0
You are using pip version 8.1.2, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

↑「selenium」をインストールしてみる。

# ./test1.py 
Traceback (most recent call last):
  File "./test1.py", line 2, in <module>
    from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'

↑やっぱりエラー

# pip show selenium
---
Metadata-Version: 2.1
Name: selenium
Version: 3.141.0
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
Installer: pip
License: Apache 2.0
Location: /usr/lib/python2.7/site-packages
Requires: urllib3
Classifiers:
  Development Status :: 5 - Production/Stable
  Intended Audience :: Developers
  License :: OSI Approved :: Apache Software License
  Operating System :: POSIX
  Operating System :: Microsoft :: Windows
  Operating System :: MacOS :: MacOS X
  Topic :: Software Development :: Testing
  Topic :: Software Development :: Libraries
  Programming Language :: Python
  Programming Language :: Python :: 2.7
  Programming Language :: Python :: 3.4
  Programming Language :: Python :: 3.5
  Programming Language :: Python :: 3.6
You are using pip version 8.1.2, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

↑Location: /usr/lib/python2.7/site-packages
のところをみる。python2 用になっている。
この「Location」のところが重要

# python3.6 -m pip install selenium
Collecting selenium
  Using cached https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl
Collecting urllib3 (from selenium)
  Downloading https://files.pythonhosted.org/packages/e6/60/247f23a7121ae632d62811ba7f273d0e58972d75e58a94d329d51550a47d/urllib3-1.25.3-py2.py3-none-any.whl (150kB)
    100% |■■■■■■■■■■■■■■| 153kB 4.0MB/s 
Installing collected packages: urllib3, selenium
Successfully installed selenium-3.141.0 urllib3-1.25.3
You are using pip version 9.0.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

↑python3用に入れる

# ./test1.py 
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)

(略)

  File "/usr/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

↑違うエラーになった。

「広告」

google chromeを入れる

vi /etc/yum.repos.d/google.chrome.repo

[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
# yum install google-chrome-stable
# google-chrome --version
Google Chrome 75.0.3770.100

↑Google Chromeが入りました。

# yum install ipa-gothic-fonts ipa-mincho-fonts ipa-pgothic-fonts ipa-pmin

↑フォントを入れます。

「広告」

google Chrome driverを入れる

ChromeDriver - WebDriver for Chrome - Downloads
Current Releases If you are using Chrome version 115 or newer, please consult the Chrome for Testing availability dashbo...

にて、Google Chromeと同じバージョンのドライバーを見つける。

# wget https://chromedriver.storage.googleapis.com/75.0.3770.90/chromedriver_linux64.zip

↑同じバージョンのchrome driverを見つけて、タウンロードする。

# unzip chromedriver_linux64.zip 
Archive:  chromedriver_linux64.zip
# cp -a chromedriver /usr/local/bin/
# chromedriver --version
ChromeDriver 75.0.3770.90 (a6dcaf7e3ec6f70a194cc25e8149475c6590e025-refs/branch-heads/3770@{#1003})

↑同じバージョンがインストールされました。

「広告」

サンプル、プログラムを実行 正常稼働

# ./test1.py

と実行をしてみる。

↑test.png が作成されます(一部をトリミングしています)。

【世界で5万人が受講】実践 Python データサイエンス・オンライン講座

タイトルとURLをコピーしました