Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,49 @@
|
|
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()
|
10 |
-
|
11 |
-
#
|
12 |
-
soup = BeautifulSoup(response.
|
13 |
-
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
content_element = soup.select_one(
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
26 |
except Exception as e:
|
27 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
# Gradio ์ธํฐํ์ด์ค ์ค์
|
30 |
iface = gr.Interface(
|
31 |
-
fn=
|
32 |
-
inputs="
|
33 |
-
outputs="
|
34 |
-
title="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
|
35 |
-
description="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ URL์ ์
๋ ฅํ๋ฉด ์ ๋ชฉ๊ณผ ๋ด์ฉ์
|
36 |
)
|
37 |
|
38 |
-
|
39 |
-
iface.launch()
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
import requests
|
4 |
|
5 |
def scrape_naver_blog(url):
|
6 |
try:
|
7 |
+
# Request the webpage
|
8 |
+
response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
|
9 |
+
response.raise_for_status()
|
10 |
+
|
11 |
+
# Parse the page with BeautifulSoup
|
12 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
13 |
+
|
14 |
+
# Extract the title
|
15 |
+
title_element = soup.select_one(
|
16 |
+
"body > div:nth-of-type(7) > div:nth-of-type(1) > div:nth-of-type(2) > div:nth-of-type(2) > div:nth-of-type(2) > div:nth-of-type(1) > div:nth-of-type(1) > div > div:nth-of-type(8) > div:nth-of-type(1) > div > table:nth-of-type(2) > tbody > tr > td:nth-of-type(2) > div:nth-of-type(1) > div > div:nth-of-type(1) > div > div > div:nth-of-type(1)"
|
17 |
+
)
|
18 |
+
title = title_element.get_text(strip=True) if title_element else "Title not found"
|
19 |
+
|
20 |
+
# Extract the content
|
21 |
+
content_element = soup.select_one(
|
22 |
+
"body > div:nth-of-type(7) > div:nth-of-type(1) > div:nth-of-type(2) > div:nth-of-type(2) > div:nth-of-type(2) > div:nth-of-type(1) > div:nth-of-type(1) > div > div:nth-of-type(8) > div:nth-of-type(1) > div > table:nth-of-type(2) > tbody > tr > td:nth-of-type(2) > div:nth-of-type(1) > div > div:nth-of-type(3) > div:nth-of-type(4)"
|
23 |
+
)
|
24 |
+
content = content_element.get_text(strip=True) if content_element else "Content not found"
|
25 |
+
|
26 |
+
return {"์ ๋ชฉ": title, "๋ด์ฉ": content}
|
27 |
+
|
28 |
+
except requests.exceptions.RequestException as e:
|
29 |
+
return {"error": f"Request failed: {e}"}
|
30 |
except Exception as e:
|
31 |
+
return {"error": f"An error occurred: {e}"}
|
32 |
+
|
33 |
+
# Define Gradio interface
|
34 |
+
def display_scraper(url):
|
35 |
+
result = scrape_naver_blog(url)
|
36 |
+
if "error" in result:
|
37 |
+
return result["error"]
|
38 |
+
return f"์ ๋ชฉ:\n{result['์ ๋ชฉ']}\n\n๋ด์ฉ:\n{result['๋ด์ฉ']}"
|
39 |
|
|
|
40 |
iface = gr.Interface(
|
41 |
+
fn=display_scraper,
|
42 |
+
inputs=gr.Textbox(label="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ URL"),
|
43 |
+
outputs=gr.Textbox(label="์คํฌ๋ํ ๊ฒฐ๊ณผ"),
|
44 |
+
title="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ์คํฌ๋ํผ",
|
45 |
+
description="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ URL์ ์
๋ ฅํ๋ฉด ๋ธ๋ก๊ทธ ์ ๋ชฉ๊ณผ ๋ด์ฉ์ ์ถ์ถํฉ๋๋ค."
|
46 |
)
|
47 |
|
48 |
+
if __name__ == "__main__":
|
49 |
+
iface.launch()
|