WebScrapping,PyQt

时间:2015-09-18 22:16:23

标签: python pyqt beautifulsoup

我一直在研究一个非常简单的脚本,它从stackoverflow中的python标签获取问题标题,并在QTextBrowser中显示它们。

应用程序按预期工作(至少在开始时)但是窗口没有显示,直到它完成加载网页并且刷新按钮冻结程序,直到它加载也有办法解决这个问题吗?这是完整的代码:

#! usr/bin/env python

from PyQt4.QtGui import *
import requests
from bs4 import BeautifulSoup
import lxml
from threading import Thread

class Form(QWidget):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        self.url = "http://www.stackoverflow.com/questions/tagged/python"

        self.browser = QTextBrowser() 
        self.connectionlabel = QLabel()

        self.refreshBtn = QPushButton("Refresh")
        self.refreshBtn.clicked.connect(self.get)

        layout = QGridLayout()
        layout.addWidget(self.connectionlabel, 0, 0)
        layout.addWidget(self.refreshBtn, 0, 1)
        layout.addWidget(self.browser, 1, 0,1,2)

        self.setLayout(layout)
        self.setWindowTitle("StackOverflow: Python")


    def get(self):
        self.browser.clear()
        self.connectionlabel.setText("Connecting.....")
        try:
            response = requests.get(self.url)
            soup = BeautifulSoup(response.content, 'lxml')

            self.connectionlabel.setText("Connected.")

            questions = soup.find_all("a", {"class": "question-hyperlink"})

            for i, questionTitle in enumerate(questions):
                try:
                    self.browser.append("\n"+str(i+1)+". "+questionTitle.text)
                except:
                    pass

        except:
            self.connectionlabel.setText("Couldn't connect.")

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    screen = Form()
    screen.show()
    t = Thread(screen.get)
    t.deamon = True
    t.start()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:2)

UI将始终冻结,直到代码执行完成。为了避免这种情况,请使用多处理或线程,并在单独的进程/线程中调用阻塞代码。您也可以使用PyQT的QThread