ขอสอบถามแนวทางเขียน code python ในส่วนนี้หน่อยครับ

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
chakirin.bfds
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 783
ลงทะเบียนเมื่อ: 22/12/2020 10:19 am

ขอสอบถามแนวทางเขียน code python ในส่วนนี้หน่อยครับ

โพสต์ที่ยังไม่ได้อ่าน โดย chakirin.bfds »

ตอนนี้เขียนโค้ด selenium ด้วย python ให้สามารถอ่านกระทู้เเล้วเลื่อนหน้าได้เเล้วครับ เเต่โค้ดที่ผมเขียนคือเหมือน ฟิกว่าหน้านั้นจะต้องมีกระทู้ 26 กระทู้ถึงจะทำงานได้สมบูณ์ครับ หากหน้านั้นมีไม่ครบ 26 กระทู้ โปรเเกรมจะไม่ทำการอ่านลิงค์กระทู้ครับ ซึ่งผมคิดว่าน่าจะอยู่ที่ผมกำหนดcode loop ของ if next==26; เเต่เมื่อเปลี่ยนเป็น
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

ส่วนนี้code database

โค้ด: เลือกทั้งหมด

# -*- 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()
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ขอสอบถามแนวทางเขียน code python ในส่วนนี้หน่อยครับ

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

กระทู้ไม่ค่อยสือ และ เจาะจง ถึงคำถามที่อยากจะถาม เลยครับ

ควรนับ elements ออกมาก่อนว่า ที่เรา find_elements ไปเจอกี่ elements แล้วค่อย Loop ตามจำนวนที่เจอ ไม่ควร fix เป็นตัวเลขลงใน code

ทำให้ครบ Loop แล้ว หา elements ตัวถัดไป เพื่อทำงาน ตามต้องการ
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
chakirin.bfds
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 783
ลงทะเบียนเมื่อ: 22/12/2020 10:19 am

Re: ขอสอบถามแนวทางเขียน code python ในส่วนนี้หน่อยครับ

โพสต์ที่ยังไม่ได้อ่าน โดย chakirin.bfds »

mindphp เขียน: 13/01/2021 5:20 pm กระทู้ไม่ค่อยสือ และ เจาะจง ถึงคำถามที่อยากจะถาม เลยครับ

ควรนับ elements ออกมาก่อนว่า ที่เรา find_elements ไปเจอกี่ elements แล้วค่อย Loop ตามจำนวนที่เจอ ไม่ควร fix เป็นตัวเลขลงใน code

ทำให้ครบ Loop แล้ว หา elements ตัวถัดไป เพื่อทำงาน ตามต้องการ
ผมไม่รู้วิธีการเขียนโค้ดเพื่อนับ element ครับ สามารถศึกษาได้ตรงไหนได้บ้างครับ
ตอนนี้ผมได้ศึกษาข้อมูลจากลิ้งนี้มาบ้างเเล้วครับ https://www.mindphp.com/developer/pytho ... enium.html
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ขอสอบถามแนวทางเขียน code python ในส่วนนี้หน่อยครับ

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

นับจำนวน elements ที่เจอ

โค้ด: เลือกทั้งหมด

len(driver.find_elements_by_xpath(" //a[@class='topictitle'] "))
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 91