Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
import requests
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
def scrape_naver_blog(url):
|
8 |
+
try:
|
9 |
+
# Send a GET request to the URL
|
10 |
+
response = requests.get(url)
|
11 |
+
response.raise_for_status()
|
12 |
+
|
13 |
+
# Parse the HTML content
|
14 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
15 |
+
|
16 |
+
# Extract title using the provided XPath-like logic
|
17 |
+
title_element = soup.select_one('div.se-module.se-module-text span.se-fs- se-ff-')
|
18 |
+
title = title_element.get_text(strip=True) if title_element else "Title not found"
|
19 |
+
|
20 |
+
# Extract content using the provided XPath-like logic
|
21 |
+
content_element = soup.select_one('div.se-main-container')
|
22 |
+
content = content_element.get_text(strip=True) if content_element else "Content not found"
|
23 |
+
|
24 |
+
return {"title": title, "content": content}
|
25 |
+
|
26 |
+
except Exception as e:
|
27 |
+
return {"error": str(e)}
|
28 |
+
|
29 |
+
@app.route('/scrape', methods=['POST'])
|
30 |
+
def scrape():
|
31 |
+
data = request.json
|
32 |
+
url = data.get('url')
|
33 |
+
if not url:
|
34 |
+
return jsonify({"error": "URL is required"}), 400
|
35 |
+
|
36 |
+
result = scrape_naver_blog(url)
|
37 |
+
return jsonify(result)
|
38 |
+
|
39 |
+
if __name__ == '__main__':
|
40 |
+
app.run(debug=True)
|