File tree 2 files changed +25
-0
lines changed
2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change
1
+ requests
2
+ beautifulsoup4
Original file line number Diff line number Diff line change
1
+ import requests
2
+ from bs4 import BeautifulSoup
3
+
4
+ def scrape_titles (url ):
5
+ try :
6
+ response = requests .get (url )
7
+ response .raise_for_status () # Check for request errors
8
+
9
+ soup = BeautifulSoup (response .text , 'html.parser' )
10
+
11
+ # Adjust the selector to match the website's structure
12
+ titles = soup .find_all ('h2' ) # Assuming article titles are in <h2> tags
13
+
14
+ print (f"Found { len (titles )} titles:" )
15
+ for i , title in enumerate (titles , start = 1 ):
16
+ print (f"{ i } : { title .get_text (strip = True )} " )
17
+
18
+ except requests .exceptions .RequestException as e :
19
+ print (f"Error fetching data from { url } : { e } " )
20
+
21
+ if __name__ == "__main__" :
22
+ url = input ("Enter the URL of the website to scrape: " )
23
+ scrape_titles (url )
You can’t perform that action at this time.
0 commit comments