Beispiel #1
0
def get_site_text(url='https://test.api.unistream.com/help/index.html'):
    """Функция возвращает содержимое по указанному url."""

    import sys

    from PySide.QtGui import QApplication
    from PySide.QtCore import QEventLoop
    from PySide.QtWebKit import QWebSettings, QWebPage, QWebView
    from PySide.QtNetwork import QNetworkProxyFactory

    # Чтобы не было проблем запуска компов с прокси:
    QNetworkProxyFactory.setUseSystemConfiguration(True)

    QWebSettings.globalSettings().setAttribute(
        QWebSettings.DeveloperExtrasEnabled, True)

    class WebPage(QWebPage):
        def userAgentForUrl(self, url):
            return 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0'

    if QApplication.instance() is None:
        QApplication(sys.argv)

    view = QWebView()
    view.setPage(WebPage())
    view.load(url)

    # Ждем пока прогрузится страница
    loop = QEventLoop()
    view.loadFinished.connect(loop.quit)
    loop.exec_()

    doc = view.page().mainFrame().documentElement()
    print(len(doc.toOuterXml()), len(doc.toPlainText()))
    return doc.toPlainText()
Beispiel #2
0
def wait_for_signal(signal, timeout=10000):
    """Waits the given signal to be emitted, or breaks after a timeout

    This context manager wraps a method of using a nested event loop to
    wait for a signal, but additionally adds a timeout in case the signal
    was prematurely emitted (in which case we will never catch it) or if
    there is some kind of error and the signal is never emitted.

    This context manager used here is inspired by code from a blob by John
    Reaver, of which an archive can be found here:

    https://web.archive.org/web/20160126155634/http://jdreaver.com/
    posts/2014-07-03-waiting-for-signals-pyside-pyqt.html
    """
    loop = QEventLoop()

    # When the signal is caught, the loop will break
    signal.connect(loop.quit)

    # The content in the context manager will now be executed
    # The timeout doesn't start until this block is finished, so make sure
    # there is no blocking calls in the with block.
    yield

    if timeout is not None:  # Not False as possible 0ms timeout would be False
        QTimer.singleShot(timeout, loop.quit)
    loop.exec_()
    def run(self, url):
        """Функция выполняет парсинг страницы объявления"""

        self.url = url
        self.phone = None

        self.t = time.clock()

        # Загружаем страницу объявления
        self.web_page.mainFrame().load(url)

        logger.debug('Начало выполнения загрузки "{}" {:.3f} секунд'.format(
            url,
            time.clock() - self.t))

        # Ждем тут, пока закончится парсинг объявления -- все из-за ассинхронности webpage и моей лени -- хочется
        # просто в цикле запустить обработку и url, но из-за асинхронности с сигналами это не сработает -- какая-то
        # лажа происходит -- падает приложение с ошибкой где-то в QtCore.dll
        loop = QEventLoop()
        self.parse_phone_finished.connect(loop.quit)
        loop.exec_()

        logger.debug(
            'Время выполнения парсера {:.3f} секунд'.format(time.clock() -
                                                            self.t))
        self.t = None
Beispiel #4
0
 def run(self, installSignalHandlers=True):
     if self._ownApp:
         self._blockApp = self.qApp
     else:
         self._blockApp = QEventLoop()
     self.runReturn()
     self._blockApp.exec_()
Beispiel #5
0
def main():
    app = QApplication([])
    webview = QWebView()
    loop = QEventLoop()
    # 创建QEventLoop对象,该对象用于创建本地时间循环
    webview.loadFinished.connect(loop.quit)
    # QWebView对象的loadFinished回调连接了QEventLoop的quit方法,从而可以在网页加载完成之后停止事件循环
    webview.load(QUrl('http://example.webscraping.com/places/default/search'))
    loop.exec_()
    # QwebView的加载方法是异步的,执行过程会在网页加载时立即传入下一行,
    # 但我们希望等待网页加载完成,因此需要在事件循环启动时调用loop.exec_()

    webview.show()     # 调用QWebViewGUI的show()方法来显示渲染窗口,以方便调试
    frame = webview.page().mainFrame()     # 创建一个指代框架的变量
    # 使用CSS模式在框架中定位元素,然后设置搜索参数
    frame.findFirstElement('#search_term').setAttribute('Value', '.')
    frame.findFirstElement('#page_size option:checked').setPlainText('1000')
    # 使用evaluate JavaScript()方法提交事件,模拟点击事件
    frame.findFirstElement('#search').evaluateJavaScript('this.click()')
    app.exec_()  # 进入应用的事件循环,可以对表单操作进行复查,如果没有使用该方法,脚本就会直接结束

    # 等待结果,三种方法处理:
    # 1、等待一定时间,期望AJAX事件能够在此时刻之前完成
    # 2、重写Qt的网络管理器,跟踪URL请求完成的事件
    # 3、轮询网页,等待特定内容出现,下面采用第三种方法
    elements = None
    while not elements:
        app.processEvents()    # 用于给Qt事件循环执行任务的事件,比如响应点击事件和更新GUI
        elements = frame.findAllElements('#results a')
    countries = [e.toPlainText().strip() for e in elements]
    print countries
def play(file_name):
    audio.setCurrentSource(Phonon.MediaSource(file_name))
    audio.play()

    loop = QEventLoop()
    audio.finished.connect(loop.quit)
    loop.exec_()
Beispiel #7
0
 def run(self, installSignalHandlers=True):
     _assert(in_main_thread())
     if self._ownApp:
         self._blockApp = self.qApp
     else:
         self._blockApp = QEventLoop()
     self.runReturn()
     self._blockApp.exec_()
Beispiel #8
0
 def launch_list(self):
     """
     Starts the execution of the queue in consecutive order, waiting for previous process to finish
     """
     for task in self.tasks_pool:
         loop = QEventLoop()
         self.task_done.connect(loop.quit)
         self.__execute_task(task)
         loop.exec_()
Beispiel #9
0
    def load(self, url):
        logger.debug('Начата загрузка url: %s.', url)
        super().load(url)

        # Ждем пока прогрузится страница
        loop = QEventLoop()
        self.loadFinished.connect(loop.quit)
        loop.exec_()

        logger.debug('Загрузка url завершена.')
Beispiel #10
0
    def join(self):
        """Join thread.

        This method is provided for consistency with CLI executor, but it
        should not be of much use in GUI context.
        """
        loop = QEventLoop()
        self.thread.finished.connect(loop.quit)  # pylint: disable=no-member
        if not self.thread.isFinished():
            loop.exec_(flags=QEventLoop.ExcludeUserInputEvents)
Beispiel #11
0
 def testBlockingSignal(self):
     app = QCoreApplication.instance() or QCoreApplication([])
     eventloop = QEventLoop()
     emitter = Emitter()
     receiver = Receiver(eventloop)
     emitter.connect(emitter, SIGNAL("signal(int)"), receiver.receive,
                     Qt.BlockingQueuedConnection)
     emitter.start()
     retval = eventloop.exec_()
     emitter.wait()
     self.assertEqual(retval, 0)
Beispiel #12
0
 def downlaod(self, url, timeout=60):
     loop = QEventLoop()
     timer = QTimer()
     timer.setSingleShot(True)
     timer.timeout.connect(loop.quit)
     self.loadFinished.connect(loop.quit)
     self.load(QUrl(url))
     timer.start(timeout * 1000)
     loop.exec_()
     if timer.isActive():
         timer.stop()
         return self.html()
     else:
         print 'Request timed out: ' + url
Beispiel #13
0
 def open(self, url, timeout=60):
     """Wait for download to complete and return result"""
     loop = QEventLoop()
     timer = QTimer()
     timer.setSingleShot(True)
     timer.timeout.connect(loop.quit)
     self.loadFinished.connect(loop.quit)
     self.load(QUrl(url))
     timer.start(timeout * 1000)
     loop.exec_()  # delay here until download finished
     if timer.isActive():
         # downloaded successfully
         timer.stop()
         return self.html()
     else:
         # timed out
         print 'Request timed out:', url
def main():
    app = QApplication([])
    webview = QWebView()
    loop = QEventLoop()
    webview.loadFinished.connect(loop.quit)
    webview.load(QUrl('http://example.webscraping.com/search'))
    loop.exec_()

    webview.show()
    frame = webview.page().mainFrame()
    frame.findFirstElement('#search_term').setAttribute('value', '.')
    frame.findFirstElement('#page_size option:checked').setPlainText('1000')
    frame.findFirstElement('#search').evaluateJavaScript('this.click()')

    elements = None
    while not elements:
        app.processEvents()
Beispiel #15
0
    def open(self, url, timeout=60):
        """wait for download to complete and return result"""
        loop = QEventLoop()
        timer = QTimer()
        timer.setSingleShot(True)  # True表示触发定时器后,仅执行事件一次
        timer.timeout.connect(loop.quit)  # 若超时,则连接loop.quit,退出事件循环
        self.loadFinished.connect(loop.quit)
        self.load(url)
        timer.start(timeout * 1000)  # 定时器以ms为单位,设置超时时间为60s
        loop.exec_()  # 等待网页加载完成后,在执行后面的代码

        if timer.isActive():
            # downloaded successfully
            timer.stop()
            return self.html()
        else:
            # timed out
            print 'Request timed out:', url
Beispiel #16
0
def main():
    app = QApplication([])
    webview = QWebView()
    loop = QEventLoop()
    webview.loadFinished.connect(loop.quit)
    webview.load(QUrl('http://example.webscraping.com/places/default/search'))
    loop.exec_()
    webview.show()
    frame = webview.page().mainFrame()
    frame.findFirstElement('#search_term').setAttribute('value', '.')
    frame.findFirstElement('#page_size option').setPlainText('1000') #设置纯文本
    frame.findFirstElement('#search').evaluateJavaScript('this.click()')
    app.exec_()
    elements = None
    while not elements:
        app.processEvents()
        elements = frame.findAllElements('#results a')
        countries = [e.toPlainText().strip() for e in elements]
        print countries
Beispiel #17
0
    def true_init(self, project_folder, lossy_factor, color_map=None):
        self.project_folder = project_folder
        self.lossy_factor = lossy_factor
        # Use the first color map present in the project folder
        if not color_map:
            try:
                self.color_map = self.files_in_folder(self.project_folder,
                                                      'act')[0]
            except IndexError:
                error_message = 'Please put one color_palette.act in the folder'
                logger.warning(error_message)
                error_box = QMessageBox()
                # error_box.setStyleSheet(self.styleSheet())
                error_box.setWindowTitle('File error')
                error_box.setText('There is .act file missing')
                error_box.setInformativeText(error_message)
                error_box.exec_()
                return
        else:
            self.color_map = color_map

        self.loop = QEventLoop()
        self.conversion1_done.connect(self.loop.quit)
        self.conversion1_done.connect(self.gifs2lossy)
        self.conversion2_done.connect(self.gifs2damaged)
        self.conversion3_done.connect(self.handbrake)
        self.conversion4_done.connect(self.cleanup)
        if __name__ == '__main__':
            self.conversion5_done.connect(
                lambda: QTimer.singleShot(1000, qapp.quit))
        self.conversion1_done.connect(lambda: print('c1done'))
        self.conversion2_done.connect(lambda: print('c2done'))
        self.conversion3_done.connect(lambda: print('c3done'))
        self.conversion4_done.connect(lambda: print('c4done'))
        self.conversion5_done.connect(lambda: print('c5done'))
        self.avis2gif()
        self.loop.exec_()
Beispiel #18
0
def main():
    '''
    首先设置搜索参数和模拟动作事件,获取在此参数和动作下搜索后得到的网页
    然后在这网页下,获取数据
    '''
    app = QApplication([])
    webview = QWebView()
    loop = QEventLoop()
    webview.loadFinished.connect(loop.quit)
    webview.load(QUrl('http://example.webscraping.com/places/default/search'))
    loop.exec_()

    webview.show()  ## 显示渲染窗口,,可以直接在这个窗口里面输入参数,执行动作,方便调试
    frame = webview.page().mainFrame()
    ## 设置搜索参数
    # frame.findAllElements('#search_term') ##寻找所有的search_term框,返回的是列表
    # frame.findAllElements('#page_size option:checked')
    # ## 表单使用evaluateJavaScript()方法进行提交,模拟点击事件
    # frame.findAllElements('#search')

    frame.findFirstElement('#search_term').setAttribute('value',
                                                        '.')  ##第一个search_term框
    frame.findFirstElement('#page_size option:checked').setPlainText(
        '1000')  ##第一个page_size框
    ## 表单使用evaluateJavaScript()方法进行提交,模拟点击事件
    frame.findFirstElement('#search').evaluateJavaScript(
        'this.click()')  ##第一个点击框

    ## 轮询网页,等待特定内容出现
    ## 下面不断循环,直到国家链接出现在results这个div元素中,每次循环都会调用app.processEvents()
    ##用于给QT事件执行任务的时间,比如响应事件和更新GUI
    elements = None
    while not elements:
        app.processEvents()
        elements = frame.findAllElements('#results a')  ##查找下载网页内的所有a标签
    countries = [e.toPlainText().strip() for e in elements]  ##取出所有a标签内的文本内容
    print countries
Beispiel #19
0
# print(json.loads(html))
# parser = etree.XMLParser(recover=True)
# tree = etree.fromstring(html,parser)
# print(tree.cssselect('div#results a'))

# html = get_results(url, headers=headers)
# parser = etree.XMLParser(recover=True)
# tree = etree.fromstring(html,parser)
# print(tree.cssselect('#result')[0].text)

#初始化一个QApplication对象,
app = QApplication([])
#创建一个QWebView对象,用于web文档的容器
webview = QWebView()
# 创建一个QEventLoop对象,用于创建本地时间循环
loop = QEventLoop()
# loadFinished回调连接了QEventLoop的quit方法,可以在网页加载完成之后停止事件循环
webview.loadFinished.connect(loop.quit)
#将要加载的url传给QWebView,PyQt将该url的字符串封装在QUrl对象中
webview.load(QUrl(url))
# 等待网页加载完成,在事件循环启动时调用loop.exec_
loop.exec_()


# 网页加载完成后退出事件循环
# html = webview.page().mainFrame().toHtml()
# # 对加载的网页产生的HTMl进行数据抽取
# parser = etree.XMLParser(recover=True)
# tree = etree.fromstring(html,parser)
# print(tree.cssselect('#result')[0].text)
Beispiel #20
0
    def __init__(self, parent=None):
        ''' sets up the whole main window '''

        #standard init
        super(MainWindow, self).__init__(parent)

        #set the window title
        self.setWindowTitle('Open Ephys Control GUI')

        self.window_height = 700
        self.window_width = 1100

        self.screen2 = QDesktopWidget().screenGeometry(0)
        self.move(
            self.screen2.left() +
            (self.screen2.width() - self.window_width) / 2.,
            self.screen2.top() +
            (self.screen2.height() - self.window_height) / 2.)

        self.get_info()
        self.noinfo = True

        while self.noinfo:
            loop = QEventLoop()
            QTimer.singleShot(500., loop.quit)
            loop.exec_()

        subprocess.Popen('start %s' % open_ephys_path, shell=True)

        self.collect_frame.connect(self.update_frame)
        self.collect_threshed.connect(self.update_threshed)
        self.acquiring = False
        self.recording = False

        self.video_height = self.window_height * .52
        self.video_width = self.window_width * .48

        self.resize(self.window_width, self.window_height)

        #create QTextEdit window 'terminal' for receiving stdout and stderr
        self.terminal = QTextEdit(self)
        #set the geometry
        self.terminal.setGeometry(
            QRect(self.window_width * .02,
                  self.window_height * .15 + self.video_height,
                  self.video_width * .96, 150))

        #make widgets
        self.setup_video_frames()
        self.setup_thresh_buttons()

        self.overlay = True

        #create thread and worker for video processing
        self.videoThread = QThread(self)
        self.videoThread.start()
        self.videoproc_worker = VideoWorker(self)
        self.videoproc_worker.moveToThread(self.videoThread)

        self.vt_file = None
        """""" """""" """""" """""" """""" """""" """""" """
        set up menus
        """ """""" """""" """""" """""" """""" """""" """"""

        #create a QMenuBar and set geometry
        self.menubar = QMenuBar(self)
        self.menubar.setGeometry(
            QRect(0, 0, self.window_width * .5, self.window_height * .03))
        #set the QMenuBar as menu bar for main window
        self.setMenuBar(self.menubar)

        #create a QStatusBar
        statusbar = QStatusBar(self)
        #set it as status bar for main window
        self.setStatusBar(statusbar)

        #create icon toolbar with default image
        iconToolBar = self.addToolBar("iconBar.png")

        #create a QAction for the acquire button
        self.action_Acq = QAction(self)
        #make it checkable
        self.action_Acq.setCheckable(True)
        #grab an icon for the button
        acq_icon = self.style().standardIcon(QStyle.SP_MediaPlay)
        #set the icon for the action
        self.action_Acq.setIcon(acq_icon)
        #when the button is pressed, call the Acquire function
        self.action_Acq.triggered.connect(self.Acquire)

        #create a QAction for the record button
        self.action_Record = QAction(self)
        #make it checkable
        self.action_Record.setCheckable(True)
        #grab an icon for the button
        record_icon = self.style().standardIcon(QStyle.SP_DialogYesButton)
        #set the icon for the action
        self.action_Record.setIcon(record_icon)
        #when the button is pressed, call advanced_settings function
        self.action_Record.triggered.connect(self.Record)

        #create QAction for stop button
        action_Stop = QAction(self)
        #grab close icon
        stop_icon = self.style().standardIcon(QStyle.SP_MediaStop)
        #set icon for action
        action_Stop.setIcon(stop_icon)
        #when button pressed, close window
        action_Stop.triggered.connect(self.Stop)

        #show tips for each action in the status bar
        self.action_Acq.setStatusTip("Start acquiring")
        self.action_Record.setStatusTip("Start recording")
        action_Stop.setStatusTip("Stop acquiring/recording")

        #add actions to icon toolbar
        iconToolBar.addAction(self.action_Acq)
        iconToolBar.addAction(self.action_Record)
        iconToolBar.addAction(action_Stop)

        #        self.sort_button = QPushButton('Sort Now',self)
        #        self.sort_button.setGeometry(QRect(self.window_width*.85,0,self.window_width*.15,self.window_height*.05))
        #        self.sort_button.clicked.connect(self.sort_now)

        #show the window if minimized by windows
        self.showMinimized()
        self.showNormal()
        """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" ""
        """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" ""
Beispiel #21
0
 def __init__(self, display=True):
     self.app = QApplication([])
     self.view = QWebView()
     self.loop = QEventLoop()
Beispiel #22
0
    def go(self):
        self.load('https://ru.stackoverflow.com/users/login')
        self.doc.findFirst('#email').setAttribute("value", self.login)
        self.doc.findFirst('#password').setAttribute("value", self.password)
        self.doc.findFirst('#submit-button').evaluateJavaScript('this.click()')

        # После клика на submit-button ждем пока прогрузится страница
        loop = QEventLoop()
        self.loadFinished.connect(loop.quit)
        loop.exec_()

        self.load(self.question_url)

        # Проверим, что наш тег не списке тегов
        tags = self.all_tags()
        logger.debug('Все тэги: %s.', tags)

        if self.our_tag in tags:
            logger.debug('Наш тэг "%s" уже есть в списке тэгов.', self.our_tag)
            return

        # TODO: Минимальная длина заголовка 15 символов.
        # TODO: Отсутствует тело сообщения.
        # TODO: проверяка на то, что править можно

        # Кликаем на "править"
        href = self.doc.findFirst('.question .suggest-edit-post').attribute('href')
        href = urljoin(self.url().toString(), href)
        logger.debug('Ссылка на страницу редактирования вопроса: %s.', href)

        self.load(href)

        tags = self.all_tags()
        logger.debug('Все тэги: %s.', tags)

        # Уже было замечен баг (http://meta.ru.stackoverflow.com/questions/2736), когда количество тегов
        # на странице вопроса и в редакторе отличалось, поэтому добавим еще проверку
        if self.our_tag in tags:
            logger.debug('Наш тэг "%s" уже есть в списке тэгов.', self.our_tag)
            return

        # Добавление нашего тега
        self.add_tag(self.our_tag)

        tags = self.all_tags()
        logger.debug('Все тэги: %s.', tags)

        # Паранойя: проверяем, что добавление тэга прошло удачно
        if self.our_tag not in tags:
            logger.warn('Нашего тэга "%s" нет в списке тэгов, так не должно быть.', self.our_tag)
            return

        # Добавление описания изменений
        logger.debug('Добавление описания изменений.')
        js = '$("#edit-comment")[0].value = "{}";'.format('Добавлена метка: {}.'.format(self.our_tag))
        self.evaluate_java_script(js)

        quote_tags = '+'.join([quote(tag) for tag in self.all_tags()])

        # TODO: не сработало
        # Имитация последствия ввода тега
        logger.debug('Имитация последствия ввода тега.')
        js = """
        $.ajax({{
        'type': 'GET',
        'url': 'api/tags/langdiv?tags={}&_={}'
        }});
        """.format(quote_tags, int(datetime.now().timestamp() * 1000))
        self.evaluate_java_script(js)

        # Ждем немного пока выполняется ajax-запрос
        loop = QEventLoop()
        QTimer.singleShot(3 * 1000, loop.quit)
        loop.exec_()

        logger.debug('Клик на кнопку "Сохранить изменения".')
        self.doc.findFirst('#submit-button').evaluateJavaScript('this.click()')

        # После клика на submit-button ждем пока прогрузится страница
        loop = QEventLoop()
        self.loadFinished.connect(loop.quit)
        loop.exec_()