Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,39 @@
|
|
1 |
-
from flask import Flask, request, jsonify
|
2 |
-
from bs4 import BeautifulSoup
|
3 |
import requests
|
4 |
-
|
5 |
-
|
6 |
|
7 |
def scrape_naver_blog(url):
|
8 |
try:
|
9 |
-
#
|
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 |
-
#
|
21 |
-
|
22 |
-
content = content_element.get_text(strip=True) if content_element else "Content not found"
|
23 |
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
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 |
-
|
37 |
-
return jsonify(result)
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import requests
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
import gradio as gr
|
4 |
|
5 |
def scrape_naver_blog(url):
|
6 |
try:
|
7 |
+
# ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ํ์ด์ง ์์ฒญ
|
8 |
response = requests.get(url)
|
9 |
+
response.raise_for_status() # HTTP ์ค๋ฅ ํ์ธ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# BeautifulSoup์ ์ฌ์ฉํ์ฌ HTML ํ์ฑ
|
12 |
+
soup = BeautifulSoup(response.text, 'lxml')
|
|
|
13 |
|
14 |
+
# ์ ๋ชฉ ์ถ์ถ
|
15 |
+
title_xpath = '/html/body/div[7]/div[1]/div[2]/div[2]/div[2]/div[1]/div[1]/div/div[8]/div[1]/div/table[2]/tbody/tr/td[2]/div[1]/div/div[1]/div/div/div[1]'
|
16 |
+
title_element = soup.select_one('html body div div div div div div div div div div div table tbody tr td div div div div div div div')
|
17 |
+
title = title_element.get_text(strip=True) if title_element else "์ ๋ชฉ์ ์ฐพ์ ์ ์์ต๋๋ค."
|
18 |
|
19 |
+
# ๋ด์ฉ ์ถ์ถ
|
20 |
+
content_xpath = '/html/body/div[7]/div[1]/div[2]/div[2]/div[2]/div[1]/div[1]/div/div[8]/div[1]/div/table[2]/tbody/tr/td[2]/div[1]/div/div[3]/div[4]'
|
21 |
+
content_element = soup.select_one('html body div div div div div div div div div div div table tbody tr td div div div div')
|
22 |
+
content = content_element.get_text(strip=True) if content_element else "๋ด์ฉ์ ์ฐพ์ ์ ์์ต๋๋ค."
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
return f"์ ๋ชฉ: {title}\n๋ด์ฉ: {content}"
|
|
|
25 |
|
26 |
+
except Exception as e:
|
27 |
+
return f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
28 |
+
|
29 |
+
# Gradio ์ธํฐํ์ด์ค ์ค์
|
30 |
+
iface = gr.Interface(
|
31 |
+
fn=scrape_naver_blog,
|
32 |
+
inputs="text",
|
33 |
+
outputs="text",
|
34 |
+
title="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ์คํฌ๋ํ",
|
35 |
+
description="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ URL์ ์
๋ ฅํ๋ฉด ์ ๋ชฉ๊ณผ ๋ด์ฉ์ ์คํฌ๋ํํฉ๋๋ค."
|
36 |
+
)
|
37 |
+
|
38 |
+
# Gradio ์ฑ ์คํ
|
39 |
+
iface.launch()
|