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

Python スクレイピング実装時に出会ったエラー

Posted at

###↓を作成していた時に出会ったエラーたちと解決方法

##WebDriverException: Message: 'chromedriver' executable needs to be in PATH.

エラーログ
C:\Users\user\Downloads\out>capture.py
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\user\Downloads\out\capture.py", line 13, in <module>
    driver = webdriver.Chrome()
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 90, in __init__
    self.service.start()
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\service.py", line 84, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home

####事象・解決方法
エラーメッセージ通り
chromedriver.exeをPATHに通すか、実行しているpyファイルと同じフォルダにおく

##WebDriverException: Message: unknown error: cannot find Chrome binary

エラーログ
C:\Users\user\Downloads\out>capture.py
Traceback (most recent call last):
  File "C:\Users\user\Downloads\out\capture.py", line 13, in <module>
    driver = webdriver.Chrome()
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 93, in __init__
    RemoteWebDriver.__init__(
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 266, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 357, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
Stacktrace:
Backtrace:
        Ordinal0 [0x0XXXX]
        Ordinal0 [0x0XXXX]
        GetHandleVerifier [0xXXXX]
        GetHandleVerifier [0xXXXX]

####事象・解決方法
Chromeをインストール済みなのに、Chromeが見つからないと言われる。
Chromeのインストール先が↓のようなユーザ単位の場合だと発生するらしい。
C:\Users\dareka\AppData\Local\Google\Chrome\Application\

アンインストールしてからインストール先を変更して再インストールで解決

##handshake failed; returned -1, SSL error code 1, net_error -202

エラーログ
ERROR:ssl_client_socket_openssl.cc(1158)] handshake failed with ChromeDriver Chrome browser and Selenium
[XXXX:ERROR:ssl_client_socket_impl.cc(983)] handshake failed; returned -1, SSL error code 1, net_error -202

####事象・解決方法
web driver のオプションに下記を追加

op = Options()
op.add_argument('--ignore-certificate-errors')
op.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(options=op)

##UnicodeEncodeError: 'charmap' codec can't encode characters

エラーログ
C:\Users\user>capture.py
Traceback (most recent call last):
  File "C:\Users\user\capture.py", line 82, in <module>
    f.writelines(driver.page_source)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u3092' in position 0: character maps to <undefined>
Traceback (most recent call last):
  File "C:\Users\user\capture.py", line 82, in <module>
    f.write(driver.page_source)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 3149-3159: character maps to <undefined>

####事象・解決方法
fileを開くときに、encodingを指定する引数を追加

#f = open(outpudDir + str(prefix_num).zfill(3) + "_" + str(url_name) + ".html", 'w')
f = open(outpudDir + str(prefix_num).zfill(3) + "_" + str(url_name) + ".html", 'w', encoding='utf-8')
f.writelines(str(driver.page_source))
f.close()
2
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
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?