quilt code

[python] crawl - selenium 본문

daily/파이썬

[python] crawl - selenium

김뱅쇼 2023. 4. 25. 21:26

(작성중)

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from day24.daocrawl import DaoCrawl
 
#GUI 환경이용(GUI는 계속 대기하는 성질이 있음)
 
form_class = uic.loadUiType("my_crawl3.ui")[0]
 
class MyWindow(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.driver = webdriver.Chrome(executable_path='chromedriver')
        self.setupUi(self)
        self.pb.clicked.connect(self.myclick)  
        self.driver.get(url = 'https://map.kakao.com/')
        
    def myclick(self):
        bus_num = self.driver.find_element(By.CLASS_NAME, 'tit_summary').text
        stopname = self.driver.find_elements(By.CLASS_NAME, 'busstop')
        bus_id = self.driver.find_elements(By.CLASS_NAME,'busid')
        
        print(bus_id[1].text)
        
        dc = DaoCrawl()
        
        for i in range(len(stopname)):
            cnt = dc.insert(bus_num, bus_id[i].text, stopname[i+1].text)
            print(cnt)
 
        
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = MyWindow()
    myWindow.show()
    app.exec_()
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import pymysql
 
class DaoCrawl:
    def __init__(self):
        self.conn = pymysql.connect(host='localhost', user='root', password='python',
                               db='python', charset='utf8',port=3305)
         
        self.curs = self.conn.cursor(pymysql.cursors.DictCursor)
        
        
    def insert(self,bus_num,bus_id,stopname):
        sql = f"""
            insert into bus
                (bus_num,bus_id,stopname)
            values
                ('{bus_num}','{bus_id}','{stopname}')
        """
        self.curs.execute(sql)
        cnt = self.curs.rowcount
        self.conn.commit()
 
        return cnt
    
    def __del__(self):
        self.curs.close()
        self.conn.close()
 
 
if __name__ == '__main__':
    dc = DaoCrawl()
    cnt = dc.insert(233)
    print("cnt",cnt)
    
    
    
    
    
cs

'daily > 파이썬' 카테고리의 다른 글

[python] flask  (0) 2023.04.24
[iBatis] iBatis (2)  (0) 2023.03.21
[iBatis] iBatis (1)  (0) 2023.03.21
[파이썬] Day02  (0) 2023.03.15
[파이썬] Day01  (0) 2023.03.13