นี้เป็น code selenium ที่เขียนไว้
โค้ด: เลือกทั้งหมด
# -*- coding: utf-8 -*-
from selenium import webdriver #อิมพอร์ตโมดูล
import time
import psycopg2 as p
import configparser
con = None
config = configparser.ConfigParser()
config.read('admin.conf')
Username = "0000000"
password = "00000000"
driver = webdriver.Firefox()
time.sleep(2)
# โปรเเกรมเพื่อการล็อคอินเข้าเว็บ
web_mindphp = 'https://www.mindphp.com/forums/ucp.php?mode=login&redirect=viewforum.php%3Ff%3D338'
driver.get(web_mindphp)
for i in range(4):
try:
if driver.find_element_by_name("username"):
print('1')
#login = wait.until(ec.visibility_of_element_located((By.NAME, "username")))
#ActionChains(driver).move_to_element(login).perform()
x = driver.find_element_by_name("username")
x.clear()
x.send_keys(Username)
print('2')
x = driver.find_element_by_name("password")
x.clear()
x.send_keys(password)
time.sleep(3)
driver.find_element_by_name("login").click()
time.sleep(3)
print('login')
break
except:
if self.link_click("??????????????"):
print("??????????????")
break
pass
print("??????")
time.sleep(2)
page = "https://www.mindphp.com/forums/viewforum.php?f=338"
driver.get(page)
time.sleep(2)
page_copy = page
# xpath_topic = " //a[@class='topictitle'] "
# all_ids_topic = driver.find_elements_by_xpath(xpath_topic)
save_link = {}
save_link_base ={}
no = 1
next = 1
for a in driver.find_elements_by_xpath("//div[@class='pagination']/ul/li/a"):
# print(a.get_attribute('class'))
# if a.get_attribute('class') == 'button button-icon-only':
# break
if no == 104 : #ตัวหยุดการทำงานของ loopเมื่ออ่านกระทู้หมดเเล้ว(ตัวทดลองจำเป็น ยังไม่สมบูรณ์)
break
else:
print(a)
time.sleep(2)
for i in driver.find_elements_by_xpath(" //a[@class='topictitle'] "):
save_link[next] = i.get_attribute('href') +' , '+i.text
save_link_base[no] = i.get_attribute('href') +' , '+i.text #เก็บ link และ topic ของหัวข้อกระทู้ที่เปิดอ่าน
print(no) # ทดลองปริ้นดูว่าอ่านกระทู้ได้กี่กระทู้
print(i.text) # ทดลองปริ้นดูว่าอ่านหัวข้อกระทอะไรบ้าง
if next == 26: # loop การเปิดอ่านกระทู้
for n, m in save_link.items():
print(n)
print(m)
driver.get(m)
# time.sleep(2) #ค้างหน้าที่เปิด 2 วินาที
driver.get(page_copy) #หลังจากเปิดกระทู้ครบทั้งหน้าให้กลับมาหน้าที่เก็บลิ้งค์
if driver.find_elements_by_xpath("//li[@class='arrow next']"): # loop การเปิดหน้าถัดไป
page_copy = driver.find_elements_by_xpath("//li[@class='arrow next']/a[@class='button button-icon-only']")[
0].get_attribute('href') #เก็บลิ้งของหน้า
driver.find_elements_by_xpath("//li[@class='arrow next']/a[@class='button button-icon-only']")[0].click()
next = 1
no += 1
save_link = {}
else:
break
time.sleep(2)
else:
no+=1
next+=1
โค้ด: เลือกทั้งหมด
# -*- coding=utf8 -*-
import psycopg2 as p
import configparser
con = None
config = configparser.ConfigParser()
config.read('admin.conf')
host=config['options']['host']
user=config['options']['user']
database=config['options']['database']
try:
con = p.connect(host = host, database= database, user= user )
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS test_01")
print("Drop ตาราง test_01 เรียบร้อยแล้ว")
# Create table as per requirement
sql = """CREATE TABLE IF NOT EXISTS test_01(
id serial NOT NULL,
topic character varying(100) NOT NULL,
source text NOT NULL,
CONSTRAINT test_pythons_pkey PRIMARY KEY (id)
) """
cur.execute(sql)
con.commit()
print("สร้างตารางเสร็จเเล้ว")
except p.DatabaseError:
print('Error %s' %p.DatabaseError)
if con:
con.close()
ขั้นตอนต่อไปที่ต้องการทำคือ การเอา source code จากกระทู้ที่อ่านบันทึกลง database ครับ ผมขอไกด์ในส่วนนี้หน่อยครับ