LoginSignup
3
5

More than 3 years have passed since last update.

Seleniumをできるだけ軽くする方法

Posted at

要件

- いまさらですが、ラズパイもカード会社のスクレイピングにトライしました。
- ラズパイゼロをつかって、Seleniumにて、スクレイピングをしようとしたため、タイムアウトが多々発生。
- 極力軽くスクレイピングが実装できるようにしました。
- パスワードとかの管理は自己責任で。。

コード

from selenium import webdriver 
from selenium.webdriver.chrome.options import Options 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.support.ui import WebDriverWait 
import time
from bs4 import BeautifulSoup as BS 
import re
import requests

 options = Options()
 options.add_argument("no-sandbox")
 options.add_argument("--disable-extensions")
 options.add_argument("--headless")
 options.add_argument('--disable-gpu')  
 options.add_argument('--ignore-certificate-errors')
 options.add_argument('--allow-running-insecure-content')
 options.add_argument('--disable-web-security')
 options.add_argument('--disable-desktop-notifications')
 options.add_argument("--disable-extensions")
 options.add_argument('--lang=ja')
 options.add_argument('--blink-settings=imagesEnabled=false')
 options.add_argument('--disable-dev-shm-usage')
 options.add_argument('--proxy-server="direct://"')
 options.add_argument('--proxy-bypass-list=*')    
 options.add_argument('--start-maximized')
 driver = webdriver.Chrome(options=options)

 driver.get (http:///~~~)

また、読み込み範囲を縮小するために、スクレイピング直前のClassまでの読み込みする形で
タイムアウトを最大限にして、記載

 wait = WebDriverWait(driver, 300);
 element=wait.until(EC.presence_of_element_located((By.CLASS_NAME,"fluid")));

あとは、BeautifulSoupで読み込み、加工

 res = driver.page_source.encode('utf-8')
 print("loading")
 soup=BS(res,"html.parser")

これで、なんとか、タイムアウトを避けることができるようになりました。

3
5
1

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
3
5