인터넷만 연결되어있다면 일정한 시간간격으로 크롤러를 해와서 정보를 띄워주는 GUI를 만들어보았다.

 

 

더보기
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import sys
import numpy as N
 
from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5.QtCore import QTimer, QTime
import requests
from bs4 import BeautifulSoup
 
import io
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')
 
form_class = uic.loadUiType("untitled2.ui")[0]
 
 
html = req.text
soup = BeautifulSoup(html, 'html.parser')
cnt = 0
class MyWindow(QDialog, form_class):
   
   def __init__(self):
       super().__init__()
       self.setupUi(self)
       # QTimer => 이벤트루프에 변수 timer라는 타이머 등록 
       self.timer = QTimer(self)
       # 1000(1sec)*x 단위로 이벤트루프 진입시(exec_()) 타이머시작
       self.timer.start(1500*1)
       # 타이머 지정 시간마다 이벤트발생 -> connect 를 통해 임의의 timeout def 슬롯 연결
 
       # crawling 을 갱신하기 위한 타이머
       self.timer_crawler = QTimer(self)
       self.timer_crawler.start(1000*60*5)
       self.timer_crawler.timeout.connect(self.timeout_crawler)
 
   def timeout(self):
       # globar => 전역변수를 지역범위에서 사용하기위해
       global cnt
       self.textEdit.setText(result[cnt].text)
       self.textEdit_2.setText(result[cnt+1].text)
       self.textEdit_3.setText(result[cnt+2].text)
       cnt = cnt+3
       if(cnt > 20): cnt = 0
 
   def timeout_crawler(self):
       req = requests.get(url)
       html = req.text
       soup = BeautifulSoup(html, 'html.parser')
       result = soup.select('strong.news-tl > a')
 
if __name__ == "__main__":
   app = QApplication(sys.argv)
   myWindow = MyWindow()
 
   app.exec_()
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'python > crawler' 카테고리의 다른 글

BeautifulSoup 를 이용한 크롤러 - Junnnho  (0) 2019.11.14

+ Recent posts