how to write a python script to extract all links from a website?
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
how to write a python script to extract all links from a website?
Assistant
Here's a basic Python script to extract all links from a website:
pythonimport requests from bs4 import BeautifulSoup url = "https://example.com" response = requests.get(url) soup = BeautifulSoup(response.text, "html.parser") links = soup.find_all("a") for link in links: print(link["href"])This will:
- Make a request to the website URL
- Parse the HTML using BeautifulSoup
- Find all anchor tags
- Print the href attribute of each link, which contains the URL
This will print all links on the page. You can modify or extend this script to:
- Crawl multiple pages on a site
- Filter for only relative or absolute links
- Check for broken links
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.