使用 PySide 调用插槽时出错
我正在尝试抓取一个依赖 JavaScript 的网站。这是一个非常基本的网站,有一个简单的整体列表(实际上是城市名称),我不想将其复制并粘贴到 Excel 中。该列表是由javascript控制的,所以我认为我需要使用Qt4之类的东西来模拟浏览器,并且我一直在尝试PySide。
我从一些非常基本的代码开始(我改编自 此处):
#!/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()
问题是,我不太了解 PySide/Qt4。我收到此错误:
Error calling slot "_finished_loading"
我什至不确定这意味着什么。我可以解决这个问题,而无需经历漫长而艰巨的过程来弄清楚 Qt4 和 PySide 吗?这是一个简单的修复吗?
感谢您的所有意见。
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 )
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
_finished_loading
中的sys.exit( 0 )
替换为QApplication.instance().exit()
。Try replacing
sys.exit( 0 )
in_finished_loading
withQApplication.instance().exit()
.您没有将 _finished_loading 声明为插槽。为此,您需要使用 @Slot() 装饰器,如下
所示。装饰器的参数是预期函数参数的 Python 数据类型的逗号分隔列表。
You didn't declare _finished_loading as a slot. For this you need to use the @Slot() decorator like this
and so on. Arguments for the decorator is a comma-separated list of Python datatypes of expected function arguments.