I have a script, that works fine:
if __name__ == '__main__':
app = QApplication(sys.argv)
bot = GBot()
bot.search('hot tea', num=20)
if signal.signal(signal.SIGINT, signal.SIG_DFL):
sys.exit(app.exec_())
app.exec_()
When I call search(), program starts working, and and loads website:
def _loadFinished(self, ok):
current_url = self.page().currentFrame().url().toString()
if str(current_url).endswith('.com/'):
self.home_search()
else:
self.get_links_text_from_page()
if self.count >= self.desired_number_of_results:
self.close()
After load finished 1 time, it checks for another condition and desides what to do next.
At the end, after program loads multiple websites. Desired data collected in variable called self.results.
So my question is how I can return result from search(), by checking condition of loadFinished().
Another words, I need to come up with some sort of algorithm that will check if loadFinished will not load any other websites, and than search() function will return desired variable. I was thinking to create another variable self.result = False than change the condition in loadFinished() and in search() place everything in while loop, and after that return result. But it doesn’t work…
search()
def search(self, keyword, num=None, output=None):
self.keyword = keyword
if output is "json":
# need to return `self.results` ONLY after program finished. because before that,
# this variable is empty
self.load('somewebsite.com')
pass
Looks like you could use a generator here. In this
QWebViewexample,loadWebsitesis called untilStopIterationis raised, in which caseprocDoneis emitted with the number of loaded websites. The output for that signal is captured in the sloton_procDone. (The output in this case is3, because of["http://www.example.com"]*3):