if <= 26 แล้ว error เเจ้งมาว่าไม่พบ element ครับผม พอจะเเนะนำได้มั้ยครับว่าครวเเก้หรือใช้วิธีไหนดีครับ
อันนี้ตัวอย่างโค้ดที่เขียนครับ(อันนี้คือโค้ดที่ทำได้ล่าสุดคือทั้งหน้าต้องมี 26 กระทุ้ ถึงจะอ่านได้สมบูรณ์)
selenium
โค้ด: เลือกทั้งหมด
# -*- coding: utf-8 -*-
from selenium import webdriver #อิมพอร์ตโมดูล
import time
import psycopg2 as p
import configparser
from urllib.parse import urlparse
from urllib.request import urlopen, Request
con = None
config = configparser.ConfigParser()
config.read('admin.conf')
host=config['options']['host']
port=config['options']['port']
user=config['options']['user']
database=config['options']['database']
Username = config['options']['Username']
password = config['options']['password']
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)
xpath_arrow = "//li[@class='arrow next'] "
xpath_endchk='loop'
save_link = {}
save_link_base ={}
no = 1
next = 1
for a in driver.find_elements_by_xpath("//div[@class='pagination']/ul/li/a"):
if xpath_endchk == 'loop' :
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
# i.get_attribute('href')
print(no, i.text)
# print(i.get_attribute('href'))
if next == 26: # loop การเปิดอ่านกระทู้
for n, m in save_link.items():
link = m.split(" , ")[0]
toppic = m.split(" , ")[1]
print(n)
print(m)
driver.get(link)
# driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# time.sleep(2)
URL = link
req = Request(URL, headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'})
urlinfo = urlparse(URL)
html = str(urlopen(req).read())
html = html.replace("'", "(single quote)")
toppic = toppic.replace("'", "(single quote)")
#บันทึกลงDatabase
try:
con = p.connect(host=host, port = port, database=database, user=user)
cur = con.cursor()
print("เชื่อมต่อฐานข้อมูลเเล้ว")
sql = "INSERT INTO test_01(topic, source)\
VALUES ('%s','%s')"% (toppic, html)
sql = sql.encode('utf - 8')
try:
cur.execute(sql)
con.commit()
print("เพิ่มข้อมูลเรียบร้อยเเล้ว")
except:
con.rollback()
print("ข้อมูลผิดพลาด")
except p.Error:
print("ติดต่อฐานข้อมูลผิดพลาด")
if con:
con.close()
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 = {}
xpath_endchk='loop'
else:
xpath_endchk='end loop'
print(xpath_endchk)
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']
port=config['options']['port']
database=config['options']['database']
try:
#creat database
conn = p.connect(
database="postgres", user=user, host= host, port= port)
conn.autocommit = True
#Creating a cursor object using the cursor() method
cur = conn.cursor()
cur.execute("DROP DATABASE IF EXISTS selenium_test")
print("Droped database ")
#Preparing query to create a database
sql = '''CREATE database selenium_test'''
#Creating a database
cur.execute(sql)
print("Database created successfully........")
except p.DatabaseError:
print('Error %s' %p.DatabaseError)
if con:
con.close()
try:
con = p.connect(host = host, port = port , database= database, user= user )
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS test_01")
print("Droped table test_01 ")
# Create table as per requirement
sql = """CREATE TABLE IF NOT EXISTS test_01(
id serial NOT NULL,
topic text NOT NULL,
source text NOT NULL,
CONSTRAINT test_pythons_pkey PRIMARY KEY (id)
) """
sql = sql.encode('utf - 8')
cur.execute(sql)
con.commit()
print("created table test_01")
except p.DatabaseError:
print('Error %s' %p.DatabaseError)
if con:
con.close()