简体   繁体   English

使用PySide调用插槽时出错

[英]Error calling slot with PySide

I'm trying my hand at scraping a JavaScript reliant site . 我正在努力抓取JavaScript相关网站 It's a pretty basic site with a simple list of entires (names of cities, actually) that I don't want to copy and paste into Excel. 这是一个非常基本的网站,上面有一个简单的整体列表(实际上是城市名称),我不想复制并粘贴到Excel中。 The list is controlled by javascript, so I figur that I need to use something like Qt4 to emulate a browser, and I've been trying PySide. 该列表由javascript控制,因此我想我需要使用Qt4之类的东西来模拟浏览器,并且我一直在尝试PySide。

I've started with some very basic code (which I've adapted from here ): 我从一些非常基本的代码开始(我从这里改编 ):

#!/usr/bin/env python

import sys
import signal 
import argparse

from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import QWebPage


class Crawler( QWebPage ):
def __init__(self, url, file):
    QWebPage.__init__( self )
    self._url = url
    self._file = file

def crawl( self ):
    signal.signal( signal.SIGINT, signal.SIG_DFL )
    self.connect( self, SIGNAL( 'loadFinished(bool)' ), self._finished_loading )
    self.mainFrame().load( QUrl( self._url ) )

def _finished_loading( self, result ):
    file = open( self._file, 'w' )
    file.write( self.mainFrame().toHtml() )
    file.close()
    sys.exit( 0 )

def main():
    app = QApplication( sys.argv )
    args = get_args()
    crawler = Crawler( args.url, args.file )
    crawler.crawl()
    sys.exit( app.exec_() )

def get_args():
"""
Command argument parser

Returns structure:
    args.url
    args.file
"""

parser = argparse.ArgumentParser(description='Basic scraper')
parser.add_argument( '-u', '--url', dest='url', help='URL to fetch data from', default='http://www.google.com')
parser.add_argument('-f','--file', dest='file', help='Local file path to save data to', default='data.txt')

args = parser.parse_args()
return args


if __name__ == '__main__':
    main()

Problem is, I don't know PySide/Qt4 really well. 问题是,我不太了解PySide / Qt4。 I get this error: 我收到此错误:

Error calling slot "_finished_loading" 

I'm not even sure what this means. 我什至不知道这意味着什么。 Is this something I can get around without engaging in a long and arduous process of figuring out Qt4 and PySide? 我是否可以在不花费大量时间弄清楚Qt4和PySide的情况下解决这个问题? Is this a simple fix? 这是一个简单的解决方法吗?

Thanks for all input. 感谢您的所有投入。

尝试更换sys.exit( 0 )_finished_loadingQApplication.instance().exit()

You didn't declare _finished_loading as a slot. 您没有将_finished_loading声明为插槽。 For this you need to use the @Slot() decorator like this 为此,您需要像这样使用@Slot()装饰器

@Slot(str)
def _finished_loading(self, result):
    print(result)

@Slot(int, int)
def add(self, a, b):
    print(a+b)

and so on. 等等。 Arguments for the decorator is a comma-separated list of Python datatypes of expected function arguments. 装饰器的参数是逗号分隔的期望函数参数的Python数据类型列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM