Debug School

Rajesh Kumar
Rajesh Kumar

Posted on

Write a python code to scrap 3 level of links of the webistes

PreReq -

  1. pip3 install requests
  2. pip install BeautifulSoup4
  3. Save this code into
  4. Run python3
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

def scrape_links(url, depth=0, max_depth=3):
    if depth > max_depth:

        response = requests.get(url)
    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")

    soup = BeautifulSoup(response.content, "html.parser")
    print(f"Level {depth}: {url}")

    # Extract links from the page and process them recursively
    for link in soup.find_all("a", href=True):
        next_url = urljoin(url, link["href"])
        scrape_links(next_url, depth + 1, max_depth)

if __name__ == "__main__":
    starting_url = ""  # Replace with the starting URL of the website you want to scrape

Enter fullscreen mode Exit fullscreen mode

Top comments (0)