見出し画像

Google ColaboratoryでスクレイピングするときにChromeとChromedriverのバージョン違いでハマっている場合の解決法(コピペ編)

とりあえず動けばいいんだよという人のために解決用コピペ

日本語の文字化け対策
chromeとchromedriverのバージョン違い対策
バージョン。

コメントは様々なところからコピペしてきているのでその名残+chatGPTによるコメント。

# 更新を実行
!sudo apt -y update

# 日本語フォントインストール
!apt install fonts-ipafont-gothic

# ダウンロードのために必要なパッケージをインストール
!rm -f *.deb
# 足りなくなったら継ぎ足していく(今回はlibvulkan1)
!sudo apt install -y wget curl unzip libvulkan1
# 以下はChromeの依存パッケージ
!wget http://archive.ubuntu.com/ubuntu/pool/main/libu/libu2f-host/libu2f-udev_1.1.4-1_all.deb
!dpkg -i libu2f-udev_1.1.4-1_all.deb

# Chromeのインストール
!wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
!dpkg -i google-chrome-stable_current_amd64.deb

#stableversionのバージョン名だけを取得する
import json
import subprocess
import os

# curlコマンドを実行してデータを取得
curl_command = "curl -sS https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions.json"  # ここに取得したいURLを入力
completed_process = subprocess.run(curl_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

# エラーチェック
if completed_process.returncode == 0:
    # 成功した場合、データはcompleted_process.stdoutに格納されています
    data = completed_process.stdout
else:
    # エラーが発生した場合、エラーメッセージはcompleted_process.stderrに格納されています
    error_message = completed_process.stderr
    print("エラーメッセージ:")
    print(error_message)

json_data = json.loads(data)
version_number = json_data["channels"]["Stable"]["version"]
#環境変数に入れる
os.environ["VERSION_NUMBER"] = version_number

# Chrome Driverのインストール
# 再実行した際に色々残っているとエラーになるので
!rm -rf /tmp/*
!wget -N https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/$VERSION_NUMBER/linux64/chromedriver-linux64.zip -P /tmp/
!unzip -o /tmp/chromedriver-linux64.zip -d /tmp/
!cp -rf /tmp/chromedriver-linux64 /tmp/chromedriver
!chmod +x /tmp/chromedriver/chromedriver
!mv /tmp/chromedriver/chromedriver /usr/local/bin/chromedriver

!pip install selenium

バージョンチェック用

!google-chrome --version
!chromedriver --version

動作テスト用 ナンバーズ3の最新当選番号の取得
日本語文字化けもチェックできます

# numbers3の最新当選番号を取得する
# jsonでkai,date,main_num,bonus_numを返す

import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
from lxml import html

date = '' # 抽選日
main_num_list = [] # 本数字6桁を格納するリスト
bonus_num_list = [] # ボーナス数字を格納するリスト

options = webdriver.ChromeOptions()

options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')

driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver=driver, timeout=30)

driver.get("https://www.mizuhobank.co.jp/retail/takarakuji/check/numbers/numbers3/index.html")

wait.until(EC.invisibility_of_element_located((By.CLASS_NAME, 'js-now-loading')))
data = driver.page_source.encode('utf-8')
soup = BeautifulSoup(data, "html.parser")
dom = html.fromstring(str(soup))

kai1 = dom.xpath('//*[@id="mainCol"]/article/section/section/section/div/div[1]/table[1]/thead/tr/th[2]')
for i in kai1:
    line = i.text
    # line = line.replace('第','')
    # line = line.replace('回','')
    kai = line
date1 = dom.xpath('//*[@id="mainCol"]/article/section/section/section/div/div[1]/table[1]/tbody/tr[1]/td')
for i in date1:
    line = i.text
    line = line.replace('年','/')
    line = line.replace('月','/')
    line = line.replace('日','')
    date = line
num2 = driver.find_elements(By.XPATH, '//*[@id="mainCol"]/article/section/section/section/div/div[1]/table[1]/tbody/tr[2]/td/strong')
for i in num2:
    #print(i.text)
    line = i.text
    #print(line)
    main_num_list = [line[0],line[1],line[2]]
    #print(main_num_list)

d = {'kai':kai,'date':date,'main_num':main_num_list}
s = json.dumps(d,ensure_ascii=False)
print(s)

driver.quit()

print('テスト終了')

この記事が気に入ったらサポートをしてみませんか?