gpt4 book ai didi

Python段错误,使用pyqt4

转载 作者:太空宇宙 更新时间:2023-11-03 11:26:00 24 4
gpt4 key购买 nike

我写了一段代码来抓取一个 web,该 web 实际上可以与一个 url 一起使用,但是当我在 .txt 中放入超过 2 个 ursl 时,它就会告诉我“Segmentation Fault”。我不知道问题出在哪里。任何帮助将不胜感激。

import sys
import time
import gc
from bs4 import BeautifulSoup
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *


class Render(QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.mainFrame().load(QUrl(url))
self.app.exec_()

def _loadFinished(self, result):
self.frame = self.mainFrame()
#self.deleteLater()
self.app.quit()

with open('/blah/blah/blah/blah/blah.txt') as f:
urls = f.read().splitlines()

for i in urls:
r = Render(i)
soup = BeautifulSoup(unicode(r.frame.toHtml()))
summary = soup.find('div',{'style' : 'padding-top:10px;'})
tables = summary.find('tbody')
count = 0
print
for row in tables.findAll('tr'):
for cell in row.findAll('td'):
data = cell.getText()
if (count < 15):
data = data + ';'
print data,
count += 1
if (count==16):
print data
count = 0

好吧,这就是代码。在告诉我段错误之前,我得到了 with 循环的 2 次迭代...:(换句话说,我可以从 txt 的 6 个 url 中抓取 2 个。

在此先感谢您的帮助

最佳答案

我设法重现了这个问题。以下代码导致 Python 失败(在 Windows 上)。不需要所有的文件读取和 BeautifulSoup 代码:

for _ in range(3):
r = Render('google.com')

如果我确保第一个 Render 对象在创建第二个对象之前被删除,那么就没有错误:

for _ in range(3):
r = Render('google.com')
del r

我找到了 this related question这表示您不能在一个进程中拥有多个 PyQt 应用程序。我对 PyQt 不熟悉,所以不知道你是如何解决这个问题的。这可能很简单,但您需要进行一些搜索。

此外,this question has code almost identical to yours , 并且有 a very good answer显示如何创建单个 QApplication 并获取多个 url。您的问题可能应该作为重复问题关闭。

关于Python段错误,使用pyqt4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33692506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com