Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,220 +1,245 @@
|
|
| 1 |
-
import
|
| 2 |
-
import re
|
| 3 |
-
import time
|
| 4 |
import requests
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
from urllib.parse import urljoin, urlparse
|
| 7 |
-
import
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Comprehensive list of headers to look like a real browser
|
| 10 |
HEADERS = {
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
}
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
for
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
gr.Markdown(
|
| 191 |
"""
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
)
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
with gr.Column(scale=3):
|
| 210 |
-
status_logs = gr.Textbox(label="Real-time Engine Logs", lines=12, max_lines=15, interactive=False)
|
| 211 |
-
file_output = gr.File(label="Download Generated .TXT File")
|
| 212 |
-
|
| 213 |
-
submit_btn.click(
|
| 214 |
-
fn=run_scraper,
|
| 215 |
-
inputs=[url_input, max_chaps, delay_input],
|
| 216 |
-
outputs=[status_logs, file_output]
|
| 217 |
)
|
| 218 |
|
| 219 |
-
|
| 220 |
-
demo.queue().launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
from urllib.parse import urljoin, urlparse
|
| 5 |
+
import time
|
| 6 |
+
import re
|
| 7 |
+
import os
|
| 8 |
|
|
|
|
| 9 |
HEADERS = {
|
| 10 |
+
"User-Agent": (
|
| 11 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 12 |
+
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
| 13 |
+
"Chrome/124.0 Safari/537.36"
|
| 14 |
+
)
|
| 15 |
}
|
| 16 |
|
| 17 |
+
SESSION = requests.Session()
|
| 18 |
+
SESSION.headers.update(HEADERS)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# ---------- HELPERS ----------
|
| 22 |
+
|
| 23 |
+
def clean_text(text):
|
| 24 |
+
text = re.sub(r"\n{3,}", "\n\n", text)
|
| 25 |
+
text = re.sub(r"[ \t]+", " ", text)
|
| 26 |
+
return text.strip()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def is_chapter_link(text, href):
|
| 30 |
+
if not href:
|
| 31 |
+
return False
|
| 32 |
+
t = (text or "").lower()
|
| 33 |
+
h = href.lower()
|
| 34 |
+
|
| 35 |
+
keywords = [
|
| 36 |
+
"chapter",
|
| 37 |
+
"chap",
|
| 38 |
+
"episode",
|
| 39 |
+
"ep",
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
return any(k in t or k in h for k in keywords)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def fetch(url):
|
| 46 |
+
r = SESSION.get(url, timeout=20)
|
| 47 |
+
r.raise_for_status()
|
| 48 |
+
return r.text
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def extract_title(soup):
|
| 52 |
+
if soup.title:
|
| 53 |
+
return soup.title.get_text(strip=True)
|
| 54 |
+
|
| 55 |
+
h1 = soup.find("h1")
|
| 56 |
+
if h1:
|
| 57 |
+
return h1.get_text(strip=True)
|
| 58 |
+
|
| 59 |
+
return "Novel"
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def extract_chapter_content(soup):
|
| 63 |
+
selectors = [
|
| 64 |
+
".chapter-content",
|
| 65 |
+
".entry-content",
|
| 66 |
+
".text-left",
|
| 67 |
+
".chapter_body",
|
| 68 |
+
".reading-content",
|
| 69 |
+
"#chapter-content",
|
| 70 |
+
"#content",
|
| 71 |
+
".content",
|
| 72 |
+
"article",
|
| 73 |
+
]
|
| 74 |
+
|
| 75 |
+
for selector in selectors:
|
| 76 |
+
node = soup.select_one(selector)
|
| 77 |
+
if node:
|
| 78 |
+
for bad in node(["script", "style", "iframe", "ins"]):
|
| 79 |
+
bad.decompose()
|
| 80 |
+
|
| 81 |
+
txt = node.get_text("\n", strip=True)
|
| 82 |
+
if len(txt) > 300:
|
| 83 |
+
return txt
|
| 84 |
+
|
| 85 |
+
body = soup.get_text("\n", strip=True)
|
| 86 |
+
return body
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def find_next_chapter(soup, current_url):
|
| 90 |
+
candidates = []
|
| 91 |
+
|
| 92 |
+
for a in soup.find_all("a", href=True):
|
| 93 |
+
txt = a.get_text(" ", strip=True).lower()
|
| 94 |
+
href = urljoin(current_url, a["href"])
|
| 95 |
+
|
| 96 |
+
if any(
|
| 97 |
+
k in txt
|
| 98 |
+
for k in ["next", "next chapter", "continue", "→", ">>"]
|
| 99 |
+
):
|
| 100 |
+
candidates.append(href)
|
| 101 |
+
|
| 102 |
+
return candidates[0] if candidates else None
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def collect_toc_links(soup, base_url):
|
| 106 |
+
seen = set()
|
| 107 |
+
links = []
|
| 108 |
+
|
| 109 |
+
for a in soup.find_all("a", href=True):
|
| 110 |
+
txt = a.get_text(" ", strip=True)
|
| 111 |
+
href = urljoin(base_url, a["href"])
|
| 112 |
+
|
| 113 |
+
if is_chapter_link(txt, href):
|
| 114 |
+
if href not in seen:
|
| 115 |
+
seen.add(href)
|
| 116 |
+
links.append(href)
|
| 117 |
+
|
| 118 |
+
return links
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# ---------- MAIN ----------
|
| 122 |
+
|
| 123 |
+
def scrape_novel(url, max_chapters, delay_sec, filename):
|
| 124 |
+
logs = []
|
| 125 |
+
|
| 126 |
+
def log(msg):
|
| 127 |
+
logs.append(msg)
|
| 128 |
+
|
| 129 |
+
try:
|
| 130 |
+
html = fetch(url)
|
| 131 |
+
except Exception as e:
|
| 132 |
+
return "\n".join(logs + [f"Failed: {e}"]), None
|
| 133 |
+
|
| 134 |
+
soup = BeautifulSoup(html, "html.parser")
|
| 135 |
+
|
| 136 |
+
title = extract_title(soup)
|
| 137 |
+
|
| 138 |
+
if not filename.strip():
|
| 139 |
+
filename = re.sub(r"[^\w\- ]+", "", title).strip() or "novel"
|
| 140 |
+
|
| 141 |
+
all_text = []
|
| 142 |
+
|
| 143 |
+
chapter_links = collect_toc_links(soup, url)
|
| 144 |
+
|
| 145 |
+
# If no TOC found → treat URL as chapter
|
| 146 |
+
if not chapter_links:
|
| 147 |
+
chapter_links = [url]
|
| 148 |
+
|
| 149 |
+
visited = set()
|
| 150 |
+
count = 0
|
| 151 |
+
|
| 152 |
+
i = 0
|
| 153 |
+
|
| 154 |
+
while i < len(chapter_links):
|
| 155 |
+
if max_chapters and count >= max_chapters:
|
| 156 |
+
break
|
| 157 |
+
|
| 158 |
+
chapter_url = chapter_links[i]
|
| 159 |
+
|
| 160 |
+
if chapter_url in visited:
|
| 161 |
+
i += 1
|
| 162 |
+
continue
|
| 163 |
+
|
| 164 |
+
visited.add(chapter_url)
|
| 165 |
+
|
| 166 |
+
try:
|
| 167 |
+
page = fetch(chapter_url)
|
| 168 |
+
s = BeautifulSoup(page, "html.parser")
|
| 169 |
+
|
| 170 |
+
chap_title = extract_title(s)
|
| 171 |
+
chap_body = extract_chapter_content(s)
|
| 172 |
+
|
| 173 |
+
all_text.append(f"\n\n{chap_title}\n")
|
| 174 |
+
all_text.append(chap_body)
|
| 175 |
+
|
| 176 |
+
log(f"✓ {chap_title}")
|
| 177 |
+
count += 1
|
| 178 |
+
|
| 179 |
+
nxt = find_next_chapter(s, chapter_url)
|
| 180 |
+
|
| 181 |
+
if nxt and nxt not in visited:
|
| 182 |
+
chapter_links.append(nxt)
|
| 183 |
+
|
| 184 |
+
time.sleep(delay_sec)
|
| 185 |
+
|
| 186 |
+
except Exception as e:
|
| 187 |
+
log(f"✗ {chapter_url} -> {e}")
|
| 188 |
+
|
| 189 |
+
i += 1
|
| 190 |
+
|
| 191 |
+
final_text = clean_text("\n".join(all_text))
|
| 192 |
+
|
| 193 |
+
path = f"{filename}.txt"
|
| 194 |
+
|
| 195 |
+
with open(path, "w", encoding="utf-8") as f:
|
| 196 |
+
f.write(final_text)
|
| 197 |
+
|
| 198 |
+
log(f"\nDone. Chapters: {count}")
|
| 199 |
+
|
| 200 |
+
return "\n".join(logs), path
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
# ---------- UI ----------
|
| 204 |
+
|
| 205 |
+
with gr.Blocks(title="Universal Webnovel Scraper") as demo:
|
| 206 |
gr.Markdown(
|
| 207 |
"""
|
| 208 |
+
# Universal Webnovel Scraper
|
| 209 |
+
|
| 210 |
+
Paste a webnovel link.
|
| 211 |
+
Supports TOC pages + direct chapters.
|
| 212 |
+
Downloads clean TXT.
|
| 213 |
+
"""
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
url = gr.Textbox(label="Novel URL")
|
| 217 |
+
|
| 218 |
+
max_chapters = gr.Number(
|
| 219 |
+
label="Max Chapters (0 = unlimited)",
|
| 220 |
+
value=50,
|
| 221 |
+
precision=0,
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
delay_sec = gr.Slider(
|
| 225 |
+
0,
|
| 226 |
+
5,
|
| 227 |
+
value=1,
|
| 228 |
+
step=0.5,
|
| 229 |
+
label="Delay Between Requests",
|
| 230 |
)
|
| 231 |
+
|
| 232 |
+
filename = gr.Textbox(label="Filename (optional)")
|
| 233 |
+
|
| 234 |
+
btn = gr.Button("Scrape")
|
| 235 |
+
|
| 236 |
+
logs = gr.Textbox(lines=18, label="Logs")
|
| 237 |
+
file_out = gr.File(label="Download TXT")
|
| 238 |
+
|
| 239 |
+
btn.click(
|
| 240 |
+
scrape_novel,
|
| 241 |
+
inputs=[url, max_chapters, delay_sec, filename],
|
| 242 |
+
outputs=[logs, file_out],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
)
|
| 244 |
|
| 245 |
+
demo.launch()
|
|
|