bisohns / search-engine-parser

Lightweight package to query popular search engines and scrape for result titles, links and descriptions
https://search-engine-parser.readthedocs.io
460 stars 87 forks source link

RuntimeError: This event loop is already running #147

Closed Huertas97 closed 3 years ago

Huertas97 commented 3 years ago

Describe the bug I've tried the example snippet code but at "gresults = gsearch.search(*search_args)" the code throws up an error message. The same error appears using any of the search engines in the example.

To Reproduce

  import pprint

  from search_engine_parser.core.engines.bing import Search as BingSearch
  from search_engine_parser.core.engines.google import Search as GoogleSearch
  from search_engine_parser.core.engines.yahoo import Search as YahooSearch

  search_args = ('preaching to the choir', 1)
  gsearch = GoogleSearch()
  ysearch = YahooSearch()
  bsearch = BingSearch()
  gresults = gsearch.search(*search_args)
  yresults = ysearch.search(*search_args)
  bresults = bsearch.search(*search_args)
  a = {
      "Google": gresults,
      "Yahoo": yresults,
      "Bing": bresults
      }

  # pretty print the result from each engine
  for k, v in a.items():
      print(f"-------------{k}------------")
      for result in v:
          pprint.pprint(result)

  # print first title from google search
  print(gresults["titles"][0])
  # print 10th link from yahoo search
  print(yresults["links"][9])
  # print 6th description from bing search
  print(bresults["descriptions"][5])

  # print first result containing links, descriptions and title
  print(gresults[0])

Output Error

---------------------------------------------------------------------------

RuntimeError                              Traceback (most recent call last)

<ipython-input-7-9b1b2b2fc0a8> in <module>()
      9 ysearch = YahooSearch()
     10 bsearch = BingSearch()
---> 11 gresults = gsearch.search(*search_args)
     12 yresults = ysearch.search(*search_args)
     13 bresults = bsearch.search(*search_args)

2 frames

/usr/local/lib/python3.7/dist-packages/search_engine_parser/core/base.py in search(self, query, page, cache, **kwargs)
    267                     query, page, **kwargs)
    268         soup = loop.run_until_complete(
--> 269             self.get_soup(url, cache=cache))
    270         return self.get_results(soup, **kwargs)
    271 

/usr/lib/python3.7/asyncio/base_events.py in run_until_complete(self, future)
    561         """
    562         self._check_closed()
--> 563         self._check_runnung()
    564 
    565         new_task = not futures.isfuture(future)

/usr/lib/python3.7/asyncio/base_events.py in _check_runnung(self)
    521     def _check_runnung(self):
    522         if self.is_running():
--> 523             raise RuntimeError('This event loop is already running')
    524         if events._get_running_loop() is not None:
    525             raise RuntimeError(

RuntimeError: This event loop is already running

Desktop (please complete the following information):

Meaveryway commented 3 years ago

Does installing the nested asyncio solve your issue? this solution was discussed in this docs related to your particular issue. It should probably be referenced in the main Readme for future users.

okkymabruri commented 3 years ago

@Meaveryway I have same problem, I run py script not jupyter

deven96 commented 3 years ago

It's like @Meaveryway pointed out. nested_asyncio can be used in scripts too not just notebooks @okkymabruri