DeprecationWarning: There is no current event loop
are-you-serat opened this issue · 2 comments
are-you-serat commented
Hi. When I start script it gives me that error:
C:\Users\Windows 11\PycharmProjects\proxy-scraper\proxyScraper.py:191: DeprecationWarning: There is no current event loop loop = asyncio.get_event_loop()
CodeByAidan commented
Try using asyncio.run()
instead of asyncio.get_event_loop()
since you are in Python 3.7 or higher.
Here is the part you are hitting your error:
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-p",
"--proxy",
help="Supported proxy type: " + ", ".join(sorted(set([s.method for s in scrapers]))),
required=True,
)
parser.add_argument(
"-o",
"--output",
help="Output file name to save .txt file",
default="output.txt",
)
parser.add_argument(
"-v",
"--verbose",
help="Increase output verbosity",
action="store_true",
)
args = parser.parse_args()
if sys.version_info >= (3, 7) and platform.system() == 'Windows':
loop = asyncio.get_event_loop()
loop.run_until_complete(scrape(args.proxy, args.output, args.verbose))
loop.close()
elif sys.version_info >= (3, 7):
asyncio.run(scrape(args.proxy, args.output, args.verbose))
else:
loop = asyncio.get_event_loop()
loop.run_until_complete(scrape(args.proxy, args.output, args.verbose))
loop.close()
You can then update the if __name__ == "__main__":
section in the file to:
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-p",
"--proxy",
help="Supported proxy type: " + ", ".join(sorted(set([s.method for s in scrapers]))),
required=True,
)
parser.add_argument(
"-o",
"--output",
help="Output file name to save .txt file",
default="output.txt",
)
parser.add_argument(
"-v",
"--verbose",
help="Increase output verbosity",
action="store_true",
)
args = parser.parse_args()
if sys.version_info >= (3, 7) and platform.system() == 'Windows':
asyncio.run(scrape(args.proxy, args.output, args.verbose))
elif sys.version_info >= (3, 7):
loop = asyncio.get_event_loop()
loop.run_until_complete(scrape(args.proxy, args.output, args.verbose))
loop.close()
else:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(scrape(args.proxy, args.output, args.verbose))
loop.close()
seratclemency commented
Thanks. It works for me.