

To run coroutines we need to use n() base_url=' files = n(main(base_url, regex='.'))Īwesome! We have a program to extract links from a webpage! The main() program is actually a coroutine, notice the async at the beginning. if prepend_base_url: files = Running the program This is achieved with list comprehension. Lastly, you like likely want to prepend the base_url to each file. This chained list is then added to the master files list. However, what we want is a single list, this is where chain comes in handy. This returns new_files, which is a list of lists. This is fast because we don’t have to wait for each task to finish before starting the next one. Then, we run all these functions concurrently by passing them to asyncio.gather(). We first create a list of coroutines by passing each sub-directory in sub_dirs to the main() function.

The next bit of code handles sub-directories, this is where asyncio shines. This gets the files found in the base URL and adds them to the master files list. from bs4 import BeautifulSoup from itertools import chain import asyncio import aiohttp import lxml import re
#Url extractor from webpage full#
Here is the full list of imports (not including Streamlit). Regular expressions are used to filter results via regex, and chain from the built-in itertools library is used to “chain” lists together. Once we receive the HTML, BeautifulSoup and lxml facilitate parsing it. Just enter the URL in the form below and our service will extract all.

Significant speedup is achieved when searching sub-directories. This online link extractor tool lets you extract valid HREF links from a web page. The reason this speeds up the program is that we can continue doing some work while we are waiting to get HTML from a webpage in step 2. To significantly increase speed, some of the functions are asynchronous through the asyncio and aiohttp packages. If the href points to sub-directory then repeat steps 2–4.Append links to the empty if they contain a regular expression.Parse HTML for tags and extract href attribute.Create an empty list to store all the links.
