ขอไกด์ในการดึงข้อมูลจากหน้า Source code ลง Database หน่อยครับ

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

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

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

ขอไกด์ในการดึงข้อมูลจากหน้า Source code ลง Database หน่อยครับ

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

ผมได้เขียนโปรเเกรม selenium เพื่ออ่านกระทู้ต่างๆในเว็บได้เเล้วครับ ตอนนี้ผมติดปัญหาเรื่องการดึงข้อมูลจากหน้า source code ของกระทู้ เพื่อนำไปบันทึกลง database ครับ

นี้เป็น 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


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']
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()
จากโค้ด selenium ที่เขียน สารมารอ่านกระทู้ทั้งหมดได้เเล้ว พร้อมกับเก็บlink ของกระทู้ที่อ่านด้วย
ขั้นตอนต่อไปที่ต้องการทำคือ การเอา source code จากกระทู้ที่อ่านบันทึกลง database ครับ ผมขอไกด์ในส่วนนี้หน่อยครับ
ภาพประจำตัวสมาชิก
MBMoo
PHP VIP Members
PHP VIP Members
โพสต์: 25879
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

Re: ขอไกด์ในการดึงข้อมูลจากหน้า Source code ลง Database หน่อยครับ

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

ลอง สำหรับอ่าน หน้า html

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

from urllib.parse import urlparse
from urllib.request import urlopen, Request


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

Re: ขอไกด์ในการดึงข้อมูลจากหน้า Source code ลง Database หน่อยครับ

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

ลองเอามาใส่เเล้ว ติดError ครับ

code

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

# -*- 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')


Username = "chakirin.bfds"
password = "m119m119m119"

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 :
        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
            # i.get_attribute('href')
            print(no)

            # print(i.get_attribute('href'))
            print(i.text)

            if next == 26:  # loop การเปิดอ่านกระทู้
                for n, m in save_link.items():
                    print(n)
            #
                    print(m)
                    driver.get(m)
                    # driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                    # time.sleep(2)
                    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())

                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


Error ที่เกิด
/home/com003/PycharmProjects/pythonProject_selenium_test/venv/bin/python /home/com003/PycharmProjects/pythonProject_selenium_test/venv/selenium_openwebtest01.py
1
2
login
<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="6c4d1634-27e0-4268-87ac-d1f4ecb22696", element="633667f7-d0ae-4851-92b7-6d2d43ff3fc4")>
1
RoadMap - Mooziicart
2
อัพเดด MooZiiCart - อัพเดดข่าวคราวความเครื่อนไหว Change Log / Version History
3
ปัญหาที่พบบ่อยในการใช้งาน MooZiicart - Index
4
ขอวิธีการหรือขั้นตอนการรันโค้ด selenium สำหรับ ทดสอบ MooZiiCart หน่อยครับ
5
เปิดเทมเพจไม่ได้
6
มีข้อผิดพลาดเกิดขึ้น 1054 Unknown column 'ordering' in 'field list' แก้ไขยังไงครับ
7
เเก้ไขหน้าลงทะเบียน
8
อยากอัพ web ขึ้น host ทำไงคะ
9
สอบถามการตั้งค่า plugin payment omise
10
อยากใส่เมนูข้างๆTemplate ทำยังไงคะ
11
เพิ่มเนื้อหาในเมนูแล้วมัน error แก้ยังไงดีคะ
12
รายการสินค้าในเว็บไซต์ห่างกันทำไงดีคะ
13
ขนาดไซส์กับสีเสื้อให้เลือกต้องเพิ่มส่วนเสริมของ Mooziicart อะไรคะ
14
โหลด tempted mooziicart แล้วใช้ไม่ได้ค่ะ
15
ใช้ virtuemart บน Joomla 1.5 อัพเกรดมาใช้ MooZiiCart ได้ไหม
16
ใช้งาน plugin mzc notify ไม่ได้ครับ
17
หน้า admin ของ MooZiicart แก้ template เองได้ไหม
18
สอบถามการเปิดใช้งานโมดูล Filters
19
สอบถาม วิธี import product เข้า mooziicart
20
จะตั้งค่าให้มีระบบเเจ้งการโอนเงินยังไงค่ะ
21
ทำเว็บขายของ หลายภาษา เริ่มจากนี่
22
สอบถามการเพิ่มสินค้าในบล็อก template ทำยังไงคะ
23
อัปเดตเวอร์ชั่น Error
24
ติดตั้ง Plugin System MZC Multi Shop ของ mooziicart ไม่ได้
25
Module Product Labels แสดงแล้ว error
26
ดาวน์โหลด Template Opencart ไม่ได้
1
viewtopic.php?f=338&t=52159 , RoadMap - Mooziicart
Traceback (most recent call last):
File "/home/com003/PycharmProjects/pythonProject_selenium_test/venv/selenium_openwebtest01.py", line 86, in <module>
req = Request(URL, headers={
NameError: name 'URL' is not defined

Process finished with exit code 1


Process finished with exit code 1
ภาพประจำตัวสมาชิก
MBMoo
PHP VIP Members
PHP VIP Members
โพสต์: 25879
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

Re: ขอไกด์ในการดึงข้อมูลจากหน้า Source code ลง Database หน่อยครับ

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

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

Re: ขอไกด์ในการดึงข้อมูลจากหน้า Source code ลง Database หน่อยครับ

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

ก็คือ URL = ตัวแปรที่เก็บลิ้งค์กระทู้ที่เปิด ใช่มั้ย
ภาพประจำตัวสมาชิก
MBMoo
PHP VIP Members
PHP VIP Members
โพสต์: 25879
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

Re: ขอไกด์ในการดึงข้อมูลจากหน้า Source code ลง Database หน่อยครับ

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

chakirin.bfds เขียน: 08/01/2021 12:09 pm ก็คือ URL = ตัวแปรที่เก็บลิ้งค์กระทู้ที่เปิด ใช่มั้ย
Url ของหน้าที่เราต้องการจะอ่าน
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

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

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