โมดูล Random (แรนดอม) นั้นเป็นโมดูลชนิดหนึ่งที่อยู่ในภาษาPython (ไพธอน) และยังอยู่ในหลายๆภาษาโปรแกรม เช่น C++(ซีพลัสพลัส) และ จาวา ซึ่งมันเป็นโมดูลที่สามารถสุ่มค่าต่างๆที่เรากำหนดไว้ได้ เช่น ค่าตัวเลข 1-100 เราจะสามารถนำความสามารถนี้ไปใช้งานได้ในหลายๆด้านเช่น การสุ่มหาเลขผู้โชคดี การสุ่มหาลำดับตัวอักษร และอื่นๆ ซึ่งโมดูลนี้มีอยุ่แล้วใน ไพธอน แทบจะทุกเวอรชั่น เพราะเป็นโมดูลพื้นฐาน เราสามารถเรียกมาใช้โดยการอิมพอร์ตออกมาใชได้เลย
การประยุกต์ใช้งานโมดูล random (แรนดอม) กับโมดูล selenium (ซิลิเนียม)
โมดูลแรนดอมนั้นถือว่าเป็นโมดูลที่สำคัญอีกโมดูลหนึ่งทำให้มีดมดูลนี้ในแทบทุกๆภาษาโปรแกรมเลย ซึ่งโมดูลนี้สามารถนำมาประยุกต์ใช้กับโมดูล selenium (ซิลิเนียม) เช่น การทำสุ่มเปิดลิงค์ที่ได้มาจากการเสริชในกูเกิ้ล หรือ การสุ่มข้อความจากในเว็บมาแสดง
วิธีการสุ่มเปิดลิงค์ที่ได้มาจากการเสริชในกูเกิ้ลโดยใช้โมดูล selenium (ซิลิเนียม)
1.เปิดหน้าเว็บและโครงสร้างหน้าเว็บที่เราต้องการเก็บลิงค์และไตเติ้ลที่จะสุ่ม ในตัวอย่างจะเข้าถึงลิงค์และไตเติ้ลโดย XPath
2.เขียนโปรแกรมดังนี้
# -*- coding: utf-8 -*-
from selenium import webdriver
import time,random #อิมพอร์ตโมดูล
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ไว้ในตัวแปร
save_link = {} #กำหนดตัวแปรประเภาดิคชันนารี่
for i in all_ids_topic: #ลูปการเก็บลิงค์และไตเเติ้ล
save_link[i.get_attribute('href')] = i.text #key = ลิงค์ value = ไตเติ้ล
print(save_link) #ปริ้นไตเติ้ลและลิงค์
a = list(save_link.keys()) #แปลงไตเติ้ลที่อยุ่ในตัวแปรดิคชันนารีเป็นลิสต์
print(a)
driver.get(a[random.randint(0, len(a)-1)]) #เปิดลิงค์โดยการสุ่ม
ผลการัรน
{'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.manning.com/books/hello-python': 'Manning | Hello! Python', 'https://www.hackerrank.com/challenges/py-hello-world': 'Say "Hello, World!" With Python | HackerRank', '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.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.kaggle.com/colinmorris/hello-python': 'Hello, Python | Kaggle'}
['https://www.learnpython.org/en/Hello,_World!', 'https://www.programiz.com/python-programming/examples/hello-world', 'https://learnpythonthehardway.org/book/ex1.html', 'https://www.manning.com/books/hello-python', 'https://www.hackerrank.com/challenges/py-hello-world', 'https://www.digitalocean.com/community/tutorials/how-to-write-your-first-python-3-program', 'https://www.amazon.com/Hello-Python-Anthony-S-Briggs/dp/1935182080', 'https://www.guru99.com/creating-your-first-python-program.html', 'https://www.kaggle.com/colinmorris/hello-python']
อ้างอิงรูปภาพ selenium-hq