2017-11-08 27 views
0

저는 PyQt5에서 동적 웹 스크래핑을 배우려고합니다. 나는 PyQt4를위한 튜토리얼을 찾고 있었기 때문에 Qt5에 몇몇 다른 라이브러리가있다.QUrl을 사용할 수 없습니다.

import sys 
from PyQt5.QtWidgets import QApplication 
from PyQt5.QtCore import QUrl 
from PyQt5.QtWebEngineWidgets import QWebEnginePage 
import bs4 as bs 
import urllib.request 

class Client(QWebEnginePage): 
    def _init_(self, url): 
     self.app=QApplication(sys.argv) 
     QWebPage._init_(self) 
     self.loadFinished.connect(self.on_page_load) 
     self.mainFrame().load(QUrl(url)) 
     self.app.exec_() 
    def on_page_load(self): 
     self.app.quit() 

url='https://pythonprogramming.net/parsememcparseface/' 
client_response=Client(url) 
source=client_response.mainFrame().toHtml() 

#sauce=urllib.request.urlopen('https://pythonprogramming.net/sitemap.xml').read() 
soup=bs.BeautifulSoup(sauce,'xml') 
js_test=soup.find('p',class_='jstest') 
print(js_test.text) 

다음 오류가 표시되고 있습니다 :

Traceback (most recent call last): 
    File "jsp.py", line 19, in <module> 
    client_response=Client(url) 
TypeError: arguments did not match any overloaded call: 
    QWebEnginePage(parent: QObject = None): argument 1 has unexpected type 'str' 
    QWebEnginePage(QWebEngineProfile, parent: QObject = None): argument 1 has unexpected 

누군가가 저를 도와주세요!

답변

0

코드는 몇 가지 오류가 있습니다 : 2가 설치되어 있어야합니다

  • 초기화 전후 강조한다.
  • QWebEnginePage 이제 직접
  • toHtml() 기능이 더 이상 동기 없습니다 그래서 HTML을 얻을 수있는 콜백 요청합니다이었다 발생한 또 다른 변화를로드해야하는 방법으로 mainFrame()을 가지고 있지만하지 않습니다 내 수정과 함께 다시 동기입니다.

코드 :

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

class Client(QWebEnginePage): 
    toHtmlFinished = pyqtSignal() 

    def __init__(self, url): 
     self.app=QApplication(sys.argv) 
     QWebEnginePage.__init__(self) 
     self.loadFinished.connect(self.on_page_load) 
     self.load(QUrl(url)) 
     self.app.exec_() 

    def on_page_load(self): 
     self.app.quit() 

    def store_html(self, html): 
     self.html = html 
     self.toHtmlFinished.emit() 

    def get_html(self): 
     self.toHtml(self.store_html) 
     loop = QEventLoop() 
     self.toHtmlFinished.connect(loop.quit) 
     loop.exec_() 
     return self.html 

url='https://pythonprogramming.net/parsememcparseface/' 
client_response=Client(url) 
source=client_response.get_html() 

print(source) 

참고 :

+0

감사 @eyllanesc –