我正在使用PyQt5来抓取网页,这对于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()
Run Code Online (Sandbox Code Playgroud)
对于安全页面,脚本返回空白页面.回来的唯一html就是<html><head></head><body></body></html>.
我有点失落.我是否缺少与处理安全URL相关的设置?
我正在尝试提高采用列表的脚本的效率,并计算在另一个"主"列表中没有多少项(list_of_all_items).
感觉可能有一种更有效的方式来做到这一点,也许是通过某种方式结合查询?
purple_count, brown_count, blue_count = 0, 0, 0
for item in list_of_purple_items:
if item not in list_of_all_items:
purple_count += 1
for item in list_of_brown_items:
if item not in list_of_all_items:
brown_list += 1
for item in list_of_blue_items:
if item not in list_of_all_items:
blue_count += 1
Run Code Online (Sandbox Code Playgroud)
编辑:
谢谢你的帮助.我进行了快速测试,看看使用大型测试用例的最佳方法是什么:
my original: 30.21s
sets: 00.02s
filter: 30.01s
sum generator: 31.08s
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是它使用套装效率更高.
再次感谢所有人.