PyQt4未返回完整的网页内容

2024-05-13 03:38:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从所有配方.com. 因为所有的食谱都使用javascript,请求不会工作,但是PyQt4应该是。我想要的数据包含在类为='概要文件审查卡'的文章中。

当我查看返回的内容时,虽然它包含了更多的请求,但它仍然缺少我想要的部分。为什么我还没有得到完整的页面内容?

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

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()

url = 'http://allrecipes.com/cook/2010/reviews/'
# use pyqt4 to render it
r = Render(url)
# pull the page content
result = r.frame.toHtml()
#use beautifulsoup to search through the content
x = BeautifulSoup(result, 'html.parser')
#search for recipes reviewed, a is empty list, it's not finding the data I want
a = x.find_all('article', class_="profile-review-card")

更新:我已经弄清楚为什么它没有返回它应该返回的内容。当浏览器加载页面后加载所有配方用户评论页面时,会在显示审阅之前显示加载图标。我发现PyQt4在这个加载过程中卡住了。

loadingloaded

当我启动PyQt4浏览器窗口并使用

^{pr2}$

它被困在这上面了

pyqt window browser

所以很明显PyQt4无法正确加载页面!我怎么能解决这个问题?

不确定这是否与此相关,但我注意到,当我加载allrecipe配方页面时,它会工作,但返回Internet插件加载错误。是否可能有一些插件丢失是问题所在?

^{pr3}$

Tags: thefromimportselfcomappurl内容