

PythonでSeleniumを使って下記のWEBサイトをスクレイピングしています。

<https://shoply.co.jp/>

エクセルからJAN_CODEを読み込んで、サイトへJAN_CODEを検索して、画像・商品名・価格をエクセルに入れています。

エクセルデータ

[code]

    JAN_CODE,画像,商品名,価格
    4953103354739
    4953103289055
    4589591675799
    4589591675126
    4589591675430
    
[/code]

実行しましたら、最初は問題なく操作されます。  
1分ぐらい経ちますと、 **requests.exceptions.SSLError:** のエラーが表示されます。

[code]

    Traceback (most recent call last):
      File "c:\Users\test\Desktop\test1.py", line 37, in <module>
        driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(),options=options)
    
    File "C:\Users\test\AppData\Local\Programs\Python\Python39\lib\site-packages\webdriver_manager\chrome.py", line 32, in install
        driver_path = self._get_driver_path(self.driver)
      File "C:\Users\test\AppData\Local\Programs\Python\Python39\lib\site-packages\webdriver_manager\manager.py", line 19, in _get_driver_path
        binary_path = self.driver_cache.find_driver(driver)
      File "C:\Users\test\AppData\Local\Programs\Python\Python39\lib\site-packages\webdriver_manager\driver_cache.py", line 74, in find_driver
        driver_version = driver.get_version()
      File "C:\Users\test\AppData\Local\Programs\Python\Python39\lib\site-packages\webdriver_manager\driver.py", line 39, in get_version
        self.get_latest_release_version()
      File "C:\Users\test\AppData\Local\Programs\Python\Python39\lib\site-packages\webdriver_manager\driver.py", line 72, in get_latest_release_version
        resp = requests.get(
      File "C:\Users\test\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 76, in get
        return request('get', url, params=params, **kwargs)
      File "C:\Users\test\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 61, in request
        return session.request(method=method, url=url, **kwargs)
      File "C:\Users\test\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 542, in request
        resp = self.send(prep, **send_kwargs)
      File "C:\Users\test\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 655, in send
        r = adapter.send(request, **kwargs)
      File "C:\Users\test\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 514, in send
        raise SSLError(e, request=request)
    requests.exceptions.SSLError: HTTPSConnectionPool(host='chromedriver.storage.googleapis.com', port=443): Max retries exceeded with url: /LATEST_RELEASE_103.0.5060 (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1123)')))
    
    
[/code]

ドライバに問題ありますでしょうか。

pip installやpythonのpackagesを最新に試しても同じエラーが表示されます。

[code]

    pip install --upgrade pip
    Requirement already satisfied: pip in c:\users\test\appdata\local\programs\python\python39\lib\site-packages (22.2)
    
[/code]

[code]

    pip install pyOpenSSL --upgrade
    Requirement already satisfied: pyOpenSSL in c:\users\test\appdata\local\programs\python\python39\lib\site-packages (22.0.0)
    Requirement already satisfied: cryptography>=35.0 in c:\users\test\appdata\local\programs\python\python39\lib\site-packages (from pyOpenSSL) (37.0.4)
    Requirement already satisfied: cffi>=1.12 in c:\users\test\appdata\local\programs\python\python39\lib\site-packages (from cryptography>=35.0->pyOpenSSL) (1.14.4)
    Requirement already satisfied: pycparser in c:\users\test\appdata\local\programs\python\python39\lib\site-packages (from cffi>=1.12->cryptography>=35.0->pyOpenSSL) (2.20)
    
[/code]

import sslも入れましたが、それでもダメでした。

[code]

    import ssl
    ssl._create_default_https_context = ssl._create_unverified_context
    
[/code]

requests.exceptions.SSLErrorを解消できる方法ありますでしょうか。  
もし分かる方がいましたら、教えていただけると幸いです。

御手数ですが、よろしくお願い致します。

全体のコード

[code]

    from cmath import exp
    from email.mime import image
    import pandas as pd
    import time
    from datetime import datetime as dt, date, timedelta
    from dateutil.relativedelta import relativedelta
    from selenium import webdriver
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.common.keys import Keys
    from openpyxl import load_workbook
    import requests
    import openpyxl
    from openpyxl.drawing.image import Image
    from selenium.webdriver.chrome.options import Options
    import uuid
    import ssl
    
    ssl._create_default_https_context = ssl._create_unverified_context
    
    
    #エクセル
    file_excel=r"C:\Users\test\Desktop\test.xlsx"
    
    #画像挿入するエクセルファイルを指定
    wb = openpyxl.load_workbook(file_excel)
    ws = wb["Sheet1"]
    
    #1行ずつ読み込み
    #2行目からループを行う
    for i in range(2022,ws.max_row+1):
     
     options = Options()
     options.add_argument('--headless')
     options.add_argument('--no-sandbox')
     options.add_argument('--disable-gpu')
         
     driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(),options=options)
    
     #JAN_CODE a列を読み込む
     JAN_CODE = ws['a'+str(i)].value
    
     #画像 b列を読み込む
     image_excdel = ws['b'+str(i)].value
     
     #url指定
     #url = "https://www.google.com/search?q="+row.JAN_CODE
     url="https://shoply.co.jp/"
     #url="https://shoply.co.jp/"+str(JAN_CODE)+"の価格と最安値"
     print(url)
     
     #検索サイトを開く
     driver.get(url)
     
     #5秒待機
     time.sleep(5)
    
     #商品名検索
     shyohinmai_kensaku=driver.find_element_by_xpath('//*[@id="js-tp-sbar"]/div[2]/div/div/div[2]/input').send_keys(str(JAN_CODE))
    
     time.sleep(2)
    
     kensaku_button=driver.find_element_by_xpath('//*[@id="js-tp-sbar"]/div[2]/div/div/div[3]').click()
    
     time.sleep(5)
    
     try:
       #画像取得
       gazou=driver.find_element_by_xpath('//*[@id="result-list"]/ul/li[1]/div/a/div[3]/img')
       print(gazou)
       
       img_url = gazou.get_attribute('src')
    
       #unique作成
       unique=str(uuid.uuid4())[-6:]
       
       #画像保存
       file_image = r"C:\Users\test\Desktop\Image\Image"+unique+".jpg"
    
       response = requests.get(img_url)
       image = response.content
    
       with open(file_image, "wb") as aaa:
         aaa.write(image)
    
       #挿入する画像を指定
       img = Image(file_image)
       
       img.width = 100
       img.height = 100
       
       #画像挿入
       ws.add_image(img, 'b'+str(i))
       
       #商品名取得
       shyohinmai=driver.find_element_by_xpath('//*[@id="result-list"]/ul/li[1]/div/a/div[4]/span[2]').text
       print(shyohinmai)
       
       #Excelへ書き込み
       ws['c'+str(i)].value = shyohinmai
       
       #価格取得
       kakaku=driver.find_element_by_xpath('//*[@id="result-list"]/ul/li[1]/div/a/div[5]/span').text
       print(kakaku)
       
       #Excelへ書き込み
       ws['d'+str(i)].value = kakaku
       
       #xlsx書き出し
       wb.save(file_excel)
       
       driver.close()
     
     except:
       driver.close()
    
[/code]


CC BY-SA 4.0
2022-07-27T02:57:23.377
90224
2022-07-27T03:41:27.413
2022-07-27T03:41:27.413
3060
18859
question
python python3 selenium
PythonにてSeleniumのrequests.exceptions.SSLErrorを解消したい