จากบทความ การใช้ตัวแปรดิคชันนารีเพื่อเก็บลิงค์และไตเติ้ลที่ได้จากการใช้งานโมดูลซิลิเนียม เราได้ทดลองการใช้การเก็บลิงค์และไตเติ้ลโดยใช้โมดูล Selenium (ซิลิเนี่ยม) และใช้ภาษาไพธอน กันไปบ้างแล้วในคราวนี้เราจะได้ลองกับเว็บอื่นๆบ้างๆซึ่งฟอร์มการเขียนโปรแกรมค่อนข้างคล้ายกันมากๆ
กูเกิ้ลเสริช กับ โมดูล selenium (ซิลิเนียม)
กูเกิ้ลเสริช คือเสริชเอ็นจิ้น ชนิดหนึ่งที่เราสามารถค้นหาสิ่งต่างๆที่เราต้องการได้ซึ่งถื่อว่าเป็นเสริชเอ็นจิ้นที่มีคุรภาพและผู้คนใช้งานกันอย่างมากมายและกว้างขวาง โดยในบทความนี้จะมาแนะนำหารใช้งานโมดูลซิลิเนียมกับกูเกิ้ลเสริชเอ็นจิ้น เพื่อเก็บลิงค์และไตเติ้ล ของสิ่งที่เราค้นหาทั้งหมด
วิธีการเก็บลิงค์และไตเติ้ลในกูเกิ้ลเสริช โดยโมดูล selenium (ซิลิเนียม)
1.เปิดหน้าเว็บและโครงสร้างหน้าเว็บที่เราต้องการเก็บลิงค์และไตเติ้ล ในตัวอย่างจะเข้าถึงลิงค์และไตเติ้ลโดย XPath
2.เขียนโปนแกรม
# -*- coding: utf-8 -*-
from selenium import webdriver
import time
driver = webdriver.Firefox(executable_path=r'C:\Users\BDC-CLUB\PycharmProjects\mdsoft\geckodriver.exe')
driver.get('https://www.google.com/') #เปิดหน้าเว้บที่ต้องการเก็บลิงค์และไตเติ้ล
a = driver.find_element_by_class_name('gsfi') #เข้าถึลงลิงค์และไตเติ้ลโดย XPath
a.send_keys("hello python") #เสริชคำว่า "hello python"
a.submit()
time.sleep(3)
#xpath_topic= "//a[@class='topictitle']" #x path ของไตเติ้ล
xpath_topic= "//div//h3//a[@href]"
all_ids_topic = driver.find_elements_by_xpath(xpath_topic) #เก็บ sessionและelementไว้ในตัวแปร
print(all_ids_topic)
save_link = {} #กำหนดตัวแปรประเภาดิคชันนารี่
for i in all_ids_topic: #ลูปการเก็บลิงค์และไตเเติ้ล
save_link[i.get_attribute('href')] = i.text
print(save_link) #ปริ้นไตเติ้ลและลิงค์
ผลการรัน:บรรทัดที่ 2 จะเก็บลิงค์และไตเติ้ลไว้
[<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="be488680-fd78-415b-a63c-738e4b3cfa1d", element="4aa2c9a6-6f49-40e7-a899-c872a4f3e878")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="be488680-fd78-415b-a63c-738e4b3cfa1d", element="e96afca2-e233-43a9-b027-ac012d882b8c")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="be488680-fd78-415b-a63c-738e4b3cfa1d", element="edaad457-17ed-49e0-861e-2be6f9aa609a")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="be488680-fd78-415b-a63c-738e4b3cfa1d", element="aaa95ae7-b5f1-45d6-8d22-51cd4a6278dd")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="be488680-fd78-415b-a63c-738e4b3cfa1d", element="686a6a62-b4a1-4bf4-93af-ac3af47aee30")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="be488680-fd78-415b-a63c-738e4b3cfa1d", element="9d067a7b-1c89-41cf-8f19-ce43fd501a00")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="be488680-fd78-415b-a63c-738e4b3cfa1d", element="18082b58-c255-4dc1-931a-2e2f323edf13")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="be488680-fd78-415b-a63c-738e4b3cfa1d", element="9aedd82e-2b99-4139-8404-f9d896d88c4e")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="be488680-fd78-415b-a63c-738e4b3cfa1d", element="4df559a5-6e0d-495c-9433-5d14b7958ca0")>]
{'https://www.learnpython.org/en/Hello,_World!': 'Hello, World! - Learn Python - Free Interactive Python Tutorial', 'https://www.programiz.com/python-programming/examples/hello-world': 'Python Program to Print Hello world! - Programiz', 'https://learnpythonthehardway.org/book/ex1.html': 'Exercise 1: A Good First Program - Learn Python the Hard Way', 'https://www.hackerrank.com/challenges/py-hello-world': 'Say "Hello, World!" With Python | HackerRank', 'https://www.amazon.com/Hello-Python-Anthony-S-Briggs/dp/1935182080': 'Hello! Python: Anthony S. Briggs: 0884771505755: Amazon.com: Books', 'https://www.guru99.com/creating-your-first-python-program.html': 'Hello World: Create your First Python Program - Guru99', 'https://www.digitalocean.com/community/tutorials/how-to-write-your-first-python-3-program': 'How To Write a Hello World Program in Python 3 | DigitalOcean', 'https://www.manning.com/books/hello-python': 'Manning | Hello! Python', 'https://www.kaggle.com/colinmorris/hello-python': 'Hello, Python | Kaggle'}