开发者

Error calling slot with PySide

开发者 https://www.devze.com 2023-04-03 16:48 出处:网络
I\'m trying my hand at scraping a JavaScript reliant site.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.The list i

I'm trying my hand at scraping a JavaScript reliant site. 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. 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.

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 )
开发者_如何学C    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. 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? Is this a simple fix?

Thanks for all input.


Try replacing sys.exit( 0 ) in _finished_loading with QApplication.instance().exit().


You didn't declare _finished_loading as a slot. For this you need to use the @Slot() decorator like this

@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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号