Module Selenium Web Scraping


โมดูล Selenium เป็นโมดูลสำหรับดูดข้อมูลบนหน้าเว็ปไซต์ หรือสร้างบอทสำหรับทำอะไรบ้างอย่างบนหน้าเว็ปไซต์

อ้างอิง Selenium เวอร์ชัน 4

1. การติดตั้ง

pip install selenium

2. ดาวน์โหลด Chrome Driver

Chrome Driver: https://chromedriver.chromium.org/downloads

เวอร์ชันของ Chrome กับ Chrome Driver ต้องเป็นเวอร์ชันเดียวกัน

Webdriver ของเบราเซอร์อื่นๆ

Firefox
https://github.com/mozilla/geckodriver/releases

MS Edge
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

Opera
https://github.com/operasoftware/operachromiumdriver/releases

3. สร้าง Browser Object

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

Chrome_Service = Service(executable_path="C:\Python\M01_Selenium\chromedriver.exe")
Chrome_Browser = webdriver.Chrome(service=Chrome_Service)

URL = 'https://www.google.co.th/'
Chrome_Browser.get(URL)

4. การระบุตำแหน่งของ Element จาก XPATH

เพิ่มส่วนนี้ลงไป

from selenium.webdriver.common.by import By

โค้ดเต็ม

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

Chrome_Service = Service(executable_path="C:\Python\M01_Selenium\chromedriver.exe")
Chrome_Browser = webdriver.Chrome(service=Chrome_Service)

URL = 'https://www.google.co.th/'

Chrome_Browser.get(URL)
Chrome_Browser.find_element(By.XPATH, '//*[@id="SIvCob"]/a')

5. การหา XPATH ของ Element

ใช้ DevTool หรือ Inspect (คลิกดูรูปใหญ่)

Module Selenium Web Scraping

6. Method พื้นฐาน

ข้อความใน Element

myText = Chrome_Browser.find_element(By.XPATH, 'XPATH ของ Element').text

ลิงค์ URL

myLink = Chrome_Browser.find_element(By.XPATH, 'XPATH ของ Element').get_attribute('href')

พิมพ์ข้อความใน Text Box

String = 'Python'
Chrome_Browser.find_element(By.XPATH, 'XPATH ของ Element').send_keys(String)

คลิก

Chrome_Browser.find_element(By.XPATH, 'XPATH ของ Element').click()

7. การรอหน้าเว็ปโหลดเสร็จ

แบบง่าย

import time
...
...
Chrome_Browser.get('https://www.google.co.th')
time.sleep(10) # รอ 10 วินาที
'''
Do something.
'''

แบบสไตล์คนเขียน

import time
...
...
Chrome_Browser.get('https://www.google.co.th')

is_wait = True
max_retry = 10 # จำนวนรอบการตรวจสอบสูงสุด
count_retry = 0 # นับว่าทดสอบมากี่ครั้ง
while is_wait and count_retry < max_retry:
    try:
        Chrome_Browser.find_element(
            By.XPATH, 'XPATH ที่นำมาทดสอบ')
        is_wait = False
        print('Element found.')
    except:
        time.sleep(1) # รอ 1 วิ ก่อนการทดสอบรอบต่อไป
        count_retry = count_retry + 1
        print('Element Not found')

'''
Do something.
'''

8. ทำงานโดยไม่แสดงอินเตอร์เฟส

ประกาศ Object

Chrome_Option = webdriver.ChromeOptions()
Chrome_Option.add_argument('headless')

Chrome_Browser = webdriver.Chrome(service=Chrome_Service, options=Chrome_Option)

โค้ดตัวเต็ม

'''
Python Selenium Tutorial

01 Install
02 Download Web Driver (Chrome)
03 Start Browser
04 Find Element by XPATH
05 Get Text
06 Get Link
07 Input Text
08 Click on Element
09 Wait Load Page
10 Headless
'''

import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

URL = 'https://www.google.co.th/'

Chrome_Service = Service(
    executable_path="C:\Python\M01_Selenium\chromedriver.exe")
Chrome_Option = webdriver.ChromeOptions()
Chrome_Option.add_argument('headless')
Chrome_Browser = webdriver.Chrome(service=Chrome_Service, options=Chrome_Option)

Chrome_Browser.get(URL)

Get_Text = Chrome_Browser.find_element(By.XPATH, '//*[@id="SIvCob"]/a').text
print(Get_Text)

Get_Link = Chrome_Browser.find_element(
    By.XPATH, '//*[@id="SIvCob"]/a').get_attribute('href')
print(Get_Link)

# Input Box
Chrome_Browser.find_element(
    By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input').send_keys('Python')

# Improve Wait
is_wait = True
max_retry = 10
count_retry = 0

while is_wait and count_retry < max_retry:
    try:
        Chrome_Browser.find_element(
            By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[2]/div[2]/div[5]/center/input[1]')
        is_wait = False
        print('Element found.')
    except:
        time.sleep(1)
        count_retry = count_retry + 1
        print('Element Not found')


# Click
Chrome_Browser.find_element(
    By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[2]/div[2]/div[5]/center/input[1]').click()


time.sleep(1000)