File tree Expand file tree Collapse file tree
Web scraping beautifulsoup Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments