Python对javascript网页的抓取仅对https页面失败

2024-03-28 10:17:43 发布

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

我使用PyQt5来抓取web页面,这对于http://url非常有效,但是对于https://url却一点都不起作用。在

我的脚本的相关部分如下:

class WebPage(QWebPage):
    def __init__(self):
        super(WebPage, self).__init__()

        self.timerScreen = QTimer()
        self.timerScreen.setInterval(2000)
        self.timerScreen.setSingleShot(True)
        self.timerScreen.timeout.connect(self.handleLoadFinished)

        self.loadFinished.connect(self.timerScreen.start)


    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):
        url = self.mainFrame().url().toString()
        html = self.mainFrame().toHtml()

        #Do stuff with html
        print('loaded: [%d bytes] %s' % (self.bytesReceived(), url))

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

对于安全页,脚本返回一个空白页。返回的唯一html是<html><head></head><body></body></html>。在

我有点不知所措。有没有一个我缺少的关于处理安全URL的设置?在


Tags: self脚本trueurlinitdefhtmlconnect
3条回答

用PyQt4测试,用HTTPS正常打开页面

import sys
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView

class Browser(QWebView):
    def __init__(self):
        QWebView.__init__(self)
        self.loadFinished.connect(self._result_available)

    def _result_available(self, ok):
        frame = self.page().mainFrame()
        print(frame.toHtml())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = Browser()
    view.load(QUrl('https://www.google.com'))
    app.exec_()

由于您的代码可以很好地处理HTTP页面,但不能处理HTTPS,所以我认为这可能是由于SSL问题。在

所以,请仔细检查一下你的PyQt5版本是否支持SSL。。。(您可以找到有关下载、安装和设置opensslhere的更多信息)

All that you need to do after downloading the SSL libraries is to ensure that Qt can find where these openSSL libraries are located

你用什么操作系统? PyQt5是用SSL支持构建的吗? 是否安装了Openssl?在

如果您在windows上,请尝试以下操作: Build PyQt5 on Windows with OpenSSL support?

你考虑过使用Beautiful Soup或{a3}吗

我在我的项目中使用了美丽的汤,而且效果很好。它也支持SSL。在

相关问题 更多 >