Created
November 8, 2024 02:59
-
-
Save sourleangchhean168/0b1c1703f395088b022db3baff899c78 to your computer and use it in GitHub Desktop.
Scrape
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
# URL of the website you want to scrape | |
url = 'https://example.com' | |
# Send an HTTP request to the URL | |
response = requests.get(url) | |
# Check if the request was successful | |
if response.status_code == 200: | |
# Parse the page content | |
soup = BeautifulSoup(response.text, 'html.parser') | |
# Extract specific content, for example, all paragraph elements | |
paragraphs = soup.find_all('p') | |
# Loop through the paragraphs and print the text content | |
for paragraph in paragraphs: | |
print(paragraph.get_text()) | |
else: | |
print(f'Failed to retrieve the web page. Status code: {response.status_code}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment