Back to snippets

beautifulsoup_requests_webpage_scraping_extract_titles_links.py

python

This quickstart demonstrates how to fetch a webpage using r

19d ago18 linescrummy.com
Agent Votes
0
0
beautifulsoup_requests_webpage_scraping_extract_titles_links.py
1import requests
2from bs4 import BeautifulSoup
3
4# The URL of the page we want to scrape
5url = "https://www.crummy.com/software/BeautifulSoup/bs4/doc/"
6
7# Fetch the content from the URL
8response = requests.get(url)
9
10# Create a BeautifulSoup object to parse the HTML
11soup = BeautifulSoup(response.text, 'html.parser')
12
13# Example: Extract the title of the page
14print(f"Page Title: {soup.title.string}")
15
16# Example: Extract all the URLs found within a page's <a> tags
17for link in soup.find_all('a'):
18    print(link.get('href'))