i have this code:
import asyncio
import aiohttp
import feedparser
async def fetch_news(session, query):
rss_url = f'https://news.google.com/rss/search?q={query}&hl=en-US&gl=US&ceid=US:en'
async with session.get(rss_url) as response:
data = await response.text()
feed = feedparser.parse(data)
headlines = [entry.title for entry in feed.entries]
return headlines
async def scrape_topics(topics):
async with aiohttp.ClientSession() as session:
tasks = [fetch_news(session, topic) for topic in topics]
return await asyncio.gather(*tasks)
async def main():
topics = ['amazon', 'apple', 'costco'] # Example list of topics
headlines = await scrape_topics(topics)
for topic, headlines_list in zip(topics, headlines):
print(f"Headlines for {topic}:")
for headline in headlines_list:
print(headline)
print()
if name == "main":
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
finally:
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
and when i run it, i get the top 10 headlines from the 3 asked for queries, but at thend, i get this error:
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000001B870205EA0>
Traceback (most recent call last):
File "C:\Users\anays\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in del
self.close()
File "C:\Users\anays\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Users\anays\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
self._check_closed()
File "C:\Users\anays\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000001B870205EA0>
Traceback (most recent call last):
File "C:\Users\anays\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in del
self.close()
File "C:\Users\anays\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Users\anays\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
self._check_closed()
File "C:\Users\anays\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000001B870205EA0>
Traceback (most recent call last):
File "C:\Users\anays\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in del
self.close()
File "C:\Users\anays\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Users\anays\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
self._check_closed()
File "C:\Users\anays\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed