LoginSignup
0
1

Seleniumの環境構築のAttributeError: 'NoneType' object has no attribute 'split'に苦戦した

Last updated at Posted at 2023-11-03

結論

Ubuntuにもchromeを入れよう。

経緯

・当初、BeautifulSoupだけでやろうと思ったが、ページ内遷移後のスクレイピングもしたいため、Seleniumも採用した
・できあがったら、Webサービスとして公開しようか悩んでいる

環境構成

wsl + Ubuntu(20.04)
Django 4.2.7
BeautifulSoup 4.12.2
Selenium 4.15.1

ディレクトリ構成

├── __init__.py
├── __pycache__
│   ├── __init__.cpython-38.pyc
│   ├── admin.cpython-38.pyc
│   ├── apps.cpython-38.pyc
│   ├── models.cpython-38.pyc
│   └── views.cpython-38.pyc
├── admin.py
├── apps.py
├── chromedriver.exe
├── chromedriver.exe:Zone.Identifier
├── migrations
│   ├── __init__.py
│   └── __pycache__
│       └── __init__.cpython-38.pyc
├── models.py
├── tests.py
└── views.py

ソースコード

views.py
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
def scraping(request):
  # ③でだめだった書き方↓
    # service = Service(executable_path=r"C:\chromedriver-win64\chromedriver.exe")
    
    # ③でひとまずエラー解消された書き方↓
    driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
    return HttpResponse("Hello, World!")

対応

ChromeDriverをchromeと同じバージョンのものをダウンロード

➁最初、Cドライブ配下に設置してたが「Chromeドライバーありません^^」エラーが出続けるため、Djangoアプリ内に配置。ディレクトリ構成には見えてないが、views.pyと同じ位置関係
→それでもエラー解消されず

➂書き方を変更
executable_pathはselenium4.6以降(?)使えないらしい。
参考URL➀:Selenium4 WebDriver の executable_path 使用に伴う警告の回避方法

本件のエラー発出。下の参考3件で解決。
※ちなみに私は参考➁では解決しなかった。
参考URL➁:kmeans.fitで AttributeError: 'NoneType' object has no attribute 'split'が出るとき
参考URL➂:UbuntuにChromeをインストールする
参考URL➃:webdriver_managerで自動的にSeleniumとChromeバージョンを一致させる

どうでもいい補足:python全然知見ないけど、すごそう(小並感)
以上

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