Skip to content

Commit bcefe73

Browse files
Merge pull request #432 from Abhinavcode13/patch-2
Create beautifulsoup.py
2 parents 5c630b0 + 4695fcd commit bcefe73

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import requests
2+
import beautifulsoup4
3+
4+
import requests
5+
from bs4 import BeautifulSoup
6+
7+
url = "https://www.example.com/technology"
8+
9+
# Send an HTTP GET request to the URL
10+
response = requests.get(url)
11+
12+
# Check if the request was successful (status code 200)
13+
if response.status_code == 200:
14+
# Parse the HTML content using BeautifulSoup
15+
soup = BeautifulSoup(response.content, "html.parser")
16+
17+
# Find all the article elements
18+
articles = soup.find_all("article")
19+
20+
# Loop through each article and extract title and link
21+
for article in articles:
22+
title = article.find("h2").text.strip()
23+
link = article.find("a")["href"]
24+
print(f"Title: {title}")
25+
print(f"Link: {link}")
26+
print("---")
27+
else:
28+
print("Failed to fetch data from the website.")
29+

0 commit comments

Comments
 (0)