我基本上试图使用PyQt废弃网站以加载Javascript,我试图通过PyQt4中的代理执行请求。我看到它适用于那个问过这个问题的人:How to make request through proxy in PyQt4但是我无法使它工作,我不知道在哪里添加答案中建议的信息:
old_manager = self.page().networkAccessManager()
new_manager = MyNetworkAccessManager(old_manager)
self.page().setNetworkAccessManager(new_manager)
知道如何完成此代码吗?
我尝试过类似的东西:
import sys
import socket
import requests
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *
from bs4 import BeautifulSoup
class MyNetworkAccessManager(QNetworkAccessManager):
def __init__(self):
QNetworkAccessManager.__init__(self)
proxy = QNetworkProxy('HTTP','179.179.253.147', '8080')
self.setProxy(proxy)
class MySettings(QWebPage):
def __init__(self):
QWebPage.__init__(self)
self.settings().setAttribute(QWebSettings.AutoLoadImages, False)
class Browser(QWebView):
def __init__(self):
QWebView.__init__(self)
old_manager = self.page().networkAccessManager()
new_manager = MyNetworkAccessManager(old_manager)
self.page().setNetworkAccessManager(new_manager)
self.setPage(MySettings())
self.loadProgress.connect(self._progress)
self.loadFinished.connect(self._loadFinished)
self.doc = self.page().currentFrame()
def _progress(self, progress):
print progress
def _loadFinished(self):
soup = BeautifulSoup(unicode(self.doc.toHtml()), 'lxml')
print soup.prettify().encode('utf-8')
if __name__ == "__main__":
app = QApplication(sys.argv)
br = Browser()
url = QUrl('https://www.example.com')
br.load(url)
br.show()
app.exec_()
但是返回错误说:
“
__init__()
正好接受1个参数(2个给定)” new_manager = MyNetworkAccessManager(old_manager)
答案 0 :(得分:0)
您不应该将旧经理作为参数传递,您必须更改:
old_manager = self.page().networkAccessManager()
new_manager = MyNetworkAccessManager(old_manager)
self.page().setNetworkAccessManager(new_manager)
到
new_manager = MyNetworkAccessManager()
self.page().setNetworkAccessManager(new_manager)
同样改变:
proxy = QNetworkProxy('HTTP','179.179.253.147', '8080')
为:
proxy = QNetworkProxy(QNetworkProxy.HttpProxy, QString("179.179.253.147"), 8080)