Python request get title multiple times

Viewed 180

I'm trying to make a request to get the website title multiple times, but I can't seem to work out how. Where am I going wrong?

import asyncio
import requests
import aiohttp
import datetime

async def fetch(session, url):
    start_time = datetime.datetime.now()
    print(start_time)
    async with session.get(url) as response:
        return await response.text()

async def main():
    base_url = "https://Url.link"
    urls = [base_url for i in range(5)]
    tasks = []
    async with aiohttp.ClientSession() as session:
        for url in urls:
            tasks.append(fetch(session, url))
        htmls = await asyncio.gather(*tasks)
        print(htmls[:100])

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())  

I'm relatively new to python.

1 Answers

I hope to help you achieve your goal with the smallest changes

import asyncio
from numpy import imag, tile
import requests
import aiohttp
import datetime
import re

async def fetch(session, url):
    start_time = datetime.datetime.now()
    print(start_time)
    async with session.get(url) as response:
        html = await response.text()
        title = re.findall('<title>(.*)</title>',html)[0]
        return title
        

async def main():
    base_url = "https://stackoverflow.com/questions/71418821/python-request-get-title-multiple-times?noredirect=1#comment126235952_71418821"
    urls = [base_url for i in range(5)]
    tasks = []
    async with aiohttp.ClientSession() as session:
        for url in urls:
            tasks.append(fetch(session, url))
        htmls = await asyncio.gather(*tasks)
        print(htmls)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main()) 
Related