2
私は今、いくつかのJavaレンダリングされたページからWebコンテンツをスクラップするためのいくつかのコードを書こうとしています。私はPyQt5を使ってWebからいくつかの例を見つけました。しかし、Python 3.5をPyQt5.5.7とともにインストールしたのに、モジュールのインポートに失敗しました(ImportError:名前 'QWebPage'をインポートできません)。参考のために以下のコードを添付しました。誰かがこの問題を解決するために何をすべきか、またはJavaレンダリングされたWebページのコンテンツをスクラップするための他の方法を提案できる場合、非常に感謝しています。Python 3.5でPyQt5からQWebPageをインポートできません
# standard imports
import sys
# third-party imports
import requests
from bs4 import BeautifulSoup
from pyvirtualdisplay import Display
from PyQt5.QtWebEngineWidgets import QWebPage
from PyQt5.QtWidgets import QApplication
class Render(QWebPage):
"""Render HTML with PyQt5 WebKit."""
def __init__(self, html):
self.html = None
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.mainFrame().setHtml(html)
self.app.exec_()
def _loadFinished(self, result):
self.html = self.mainFrame().toHtml()
self.app.quit()
url = 'https://impythonist.wordpress.com/2015/01/06/ultimate-guide-for-scraping-javascript-rendered-web-pages/'
# get the raw HTML
source_html = requests.get(url).text
# return the JavaScript rendered HTML
with Display(visible=0, size=(800, 600)):
rendered_html = Render(source_html).html
# get the BeautifulSoup
soup = BeautifulSoup(rendered_html, 'html.parser')
print('title is %r' % soup.select_one('title').text)