gpt4 book ai didi

python - 使用 QWebPage 抓取多个 url

转载 作者:太空狗 更新时间:2023-10-30 00:56:10 25 4
gpt4 key购买 nike

我正在使用 Qt 的 QWebPage 呈现一个页面,该页面使用 javascript 动态更新其内容 - 因此仅下载静态版本页面(例如 urllib2)的库将无法工作。

我的问题是,当我呈现第二个页面时,大约 99% 的时间程序会崩溃。其他时候,它会在崩溃前工作三次。我也遇到了一些段错误,但都是随机的。

我的猜测是我用来渲染的对象没有被正确删除,所以尝试重用它可能会给我自己带来一些问题。我四处寻找,似乎没有人真正遇到同样的问题。

这是我正在使用的代码。该程序从 Steam 的社区市场下载网页,因此我可以创建所有项目的数据库。我需要多次调用 getItemsFromPage 函数来获取所有项目,因为它们被分解成页面(显示 X 数量中的 1-10 结果)。

import csv
import re
import sys
from string import replace
from bs4 import BeautifulSoup
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *

class Item:
__slots__ = ("name", "count", "price", "game")

def __repr__(self):
return self.name + "(" + str(self.count) + ")"

def __str__(self):
return self.name + ", " + str(self.count) + ", $" + str(self.price)

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.app.quit()
self.deleteLater()

def getItemsFromPage(appid, page=1):

r = Render("http://steamcommunity.com/market/search?q=appid:" + str(appid) + "#p" + str(page))

soup = BeautifulSoup(str(r.frame.toHtml().toUtf8()))

itemLst = soup.find_all("div", "market_listing_row market_recent_listing_row")

items = []

for k in itemLst:
i = Item()

i.name = k.find("span", "market_listing_item_name").string
i.count = int(replace(k.find("span", "market_listing_num_listings_qty").string, ",", ""))
i.price = float(re.search(r'\$([0-9]+\.[0-9]+)', str(k)).group(1))
i.game = appid

items.append(i)

return items

if __name__ == "__main__":

print "Updating market items to dota2.csv ..."

i = 1

with open("dota2.csv", "w") as f:
writer = csv.writer(f)

r = None

while True:
print "Page " + str(i)

items = getItemsFromPage(570)

if len(items) == 0:
print "No items found, stopping..."
break

for k in items:
writer.writerow((k.name, k.count, k.price, k.game))

i += 1

print "Done."

调用 getItemsFromPage 一次就可以了。随后的电话给了我我的问题。程序的输出通常是

Updating market items to dota2.csv ...
Page 1
Page 2

然后它崩溃了。它应该持续超过 700 页。

最佳答案

你的程序的问题是你试图用你获取的每个 url 创建一个新的 QApplication。

相反,应该只创建一个 QApplication 和一个 WebPage。 WebPage 可以使用其 loadFinished 信号创建一个内部循环,方法是在处理完每个 url 后获取一个新的 url。可以通过将用户定义的插槽连接到信号来添加自定义 html 处理,该信号在可用时发出 html 文本和 url。下面的脚本(针对 PyQt5 和 PyQt4)展示了如何实现它。

下面是一些展示如何使用 WebPage 类的示例:

用法:

def my_html_processor(html, url):
print('loaded: [%d chars] %s' % (len(html), url))

import sys
app = QApplication(sys.argv)
webpage = WebPage(verbose=False)
webpage.htmlReady.connect(my_html_processor)

# example 1: process list of urls

urls = ['https://en.wikipedia.org/wiki/Special:Random'] * 3
print('Processing list of urls...')
webpage.process(urls)

# example 2: process one url continuously
#
# import signal, itertools
# signal.signal(signal.SIGINT, signal.SIG_DFL)
#
# print('Processing url continuously...')
# print('Press Ctrl+C to quit')
#
# url = 'https://en.wikipedia.org/wiki/Special:Random'
# webpage.process(itertools.repeat(url))

sys.exit(app.exec_())

PyQt5 网页:

from PyQt5.QtCore import pyqtSignal, QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEnginePage

class WebPage(QWebEnginePage):
htmlReady = pyqtSignal(str, str)

def __init__(self, verbose=False):
super().__init__()
self._verbose = verbose
self.loadFinished.connect(self.handleLoadFinished)

def process(self, urls):
self._urls = iter(urls)
self.fetchNext()

def fetchNext(self):
try:
url = next(self._urls)
except StopIteration:
return False
else:
self.load(QUrl(url))
return True

def processCurrentPage(self, html):
self.htmlReady.emit(html, self.url().toString())
if not self.fetchNext():
QApplication.instance().quit()

def handleLoadFinished(self):
self.toHtml(self.processCurrentPage)

def javaScriptConsoleMessage(self, *args, **kwargs):
if self._verbose:
super().javaScriptConsoleMessage(*args, **kwargs)

PyQt4 网页:

from PyQt4.QtCore import pyqtSignal, QUrl
from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebPage

class WebPage(QWebPage):
htmlReady = pyqtSignal(str, str)

def __init__(self, verbose=False):
super(WebPage, self).__init__()
self._verbose = verbose
self.mainFrame().loadFinished.connect(self.handleLoadFinished)

def start(self, urls):
self._urls = iter(urls)
self.fetchNext()

def fetchNext(self):
try:
url = next(self._urls)
except StopIteration:
return False
else:
self.mainFrame().load(QUrl(url))
return True

def processCurrentPage(self):
self.htmlReady.emit(
self.mainFrame().toHtml(), self.mainFrame().url().toString())
print('loaded: [%d bytes] %s' % (self.bytesReceived(), url))

def handleLoadFinished(self):
self.processCurrentPage()
if not self.fetchNext():
QApplication.instance().quit()

def javaScriptConsoleMessage(self, *args, **kwargs):
if self._verbose:
super(WebPage, self).javaScriptConsoleMessage(*args, **kwargs)

关于python - 使用 QWebPage 抓取多个 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21274865/

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