0

So, guys, i am still confuse why the iteration always stop at the third turn
Here is my code :

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEnginePage
import sys
import numpy as np
from bs4 import BeautifulSoup as soup

class Client(QWebEnginePage):
    def __init__(self,url):
        global app
        self.app = QApplication(sys.argv)
        QWebEnginePage.__init__(self)
        self.html = ""
        self.loadFinished.connect(self.on_load_finished)
        self.load(QUrl(url))
        self.app.exec_()

    def on_load_finished(self):
        self.html = self.toHtml(self.Callable)
        print("Load Finished")

    def Callable(self,data):
        self.html = data
        self.app.quit()



linkgroup = []
linkgroup.append("https://docs.python.org/3/whatsnew/3.7.html")
linkgroup.append("https://docs.python.org/3/tutorial/index.html")
linkgroup.append("https://docs.python.org/3/installing/index.html")
linkgroup.append("https://docs.python.org/3/reference/index.html")
linkgroup.append("https://docs.python.org/3/using/index.html")

for h in range(0,len(linkgroup)):
    #Setting Url
    url = linkgroup[h]
    print(url)
    print("Loop Index : " + str(h))

    client_response = Client(url)

The output is this

https://docs.python.org/3/whatsnew/3.7.html
Loop Index : 0
Load Finished
https://docs.python.org/3/tutorial/index.html
Loop Index : 1
Load Finished
https://docs.python.org/3/installing/index.html
Loop Index : 2

As you can see, the rest iterations of the loop doesn't get executed as it doesn't show the respond from the Client class

phapha pha
  • 319
  • 1
  • 5
  • 16
  • 1
    The constructor of your `Client` class creates a new `QApplication` object. That is not a good idea, there should be only one `QApplication` object per program. – pschill Jan 10 '19 at 07:10
  • How should I do it? Is that really the reason why the loop stop? – phapha pha Jan 10 '19 at 07:13
  • 1
    Running your code I get segmentation fault on that line as well. So yes, that's exactly why it stops. – arshbot Jan 10 '19 at 07:15

1 Answers1

1

As commented by pschill above, you should have only one QApplication. How about passing it as a parameter to the constructor? Something like:

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEnginePage
import sys
import numpy as np
from bs4 import BeautifulSoup as soup

class Client(QWebEnginePage):
    def __init__(self,url,app):
        self.app = app
        QWebEnginePage.__init__(self)
        self.html = ""
        self.loadFinished.connect(self.on_load_finished)
        self.load(QUrl(url))
        self.app.exec_()

    def on_load_finished(self):
        self.html = self.toHtml(self.Callable)
        print("Load Finished")

    def Callable(self,data):
        self.html = data
        self.app.quit()

linkgroup = []
linkgroup.append("https://docs.python.org/3/whatsnew/3.7.html")
linkgroup.append("https://docs.python.org/3/tutorial/index.html")
linkgroup.append("https://docs.python.org/3/installing/index.html")
linkgroup.append("https://docs.python.org/3/reference/index.html")
linkgroup.append("https://docs.python.org/3/using/index.html")

app = QApplication(sys.argv)

for h in range(0,len(linkgroup)):
    #Setting Url
    url = linkgroup[h]
    print(url)
    print("Loop Index : " + str(h))

    client_response = Client(url, app)
millibyte
  • 77
  • 4