Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -21,6 +21,39 @@ app.add_middleware(
|
|
21 |
|
22 |
@app.get("/get_scraped_data")
|
23 |
async def get_data(url: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
try:
|
25 |
data = await Scraper.scrape(url)
|
26 |
return data
|
|
|
21 |
|
22 |
@app.get("/get_scraped_data")
|
23 |
async def get_data(url: str):
|
24 |
+
import requests
|
25 |
+
from bs4 import BeautifulSoup
|
26 |
+
|
27 |
+
# URL of the page to scrape
|
28 |
+
#url = "https://www.imf.org/en/News/Articles/2024/03/21/pr2494-sri-lanka-imf-staff-level-agreement-for-second-review-sla"
|
29 |
+
url = url
|
30 |
+
|
31 |
+
# Send a GET request to the URL
|
32 |
+
response = requests.get(url)
|
33 |
+
|
34 |
+
# Check if the request was successful
|
35 |
+
if response.status_code == 200:
|
36 |
+
# Parse the page content
|
37 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
38 |
+
|
39 |
+
# Extract all text content (paragraphs, headers, etc.)
|
40 |
+
elements = soup.find_all(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
|
41 |
+
body_text = "\n".join([element.get_text().strip() for element in elements])
|
42 |
+
|
43 |
+
# Extract all links
|
44 |
+
links = []
|
45 |
+
for a_tag in soup.find_all('a', href=True):
|
46 |
+
links.append(a_tag['href'])
|
47 |
+
|
48 |
+
# Print the extracted information
|
49 |
+
print("Body Text:")
|
50 |
+
print(body_text)
|
51 |
+
print("\nLinks:")
|
52 |
+
for link in links:
|
53 |
+
print(link)
|
54 |
+
else:
|
55 |
+
print("Failed to retrieve the webpage")
|
56 |
+
return "done"
|
57 |
try:
|
58 |
data = await Scraper.scrape(url)
|
59 |
return data
|