-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
153 lines (110 loc) · 4.56 KB
/
Main.py
File metadata and controls
153 lines (110 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
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
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
from __future__ import annotations
import logging
from contextlib import closing
from pathlib import Path
from typing import Optional, Set
import requests
from bs4 import BeautifulSoup
# Configure these values before running the script.
STARTING_URL = "https://example.com/first-post"
ENDING_URL = "https://example.com/final-post"
OUTPUT_FILE = "webserial.html"
REQUEST_TIMEOUT_SECONDS = 30
ENTRY_CONTENT_CLASS = "entry-content"
ENTRY_TITLE_CLASS = "entry-title"
LOGGER = logging.getLogger(__name__)
def configure_logging() -> None:
logging.basicConfig(level=logging.INFO, format="%(message)s")
def validate_configuration() -> None:
placeholders = {
"STARTING_URL": STARTING_URL,
"ENDING_URL": ENDING_URL,
"OUTPUT_FILE": OUTPUT_FILE,
}
for field_name, value in placeholders.items():
if not value.strip():
raise ValueError(f"{field_name} must not be empty.")
if "example.com" in STARTING_URL or "example.com" in ENDING_URL:
raise ValueError("Replace STARTING_URL and ENDING_URL with real post URLs before running the scraper.")
def log_error(message: str) -> None:
LOGGER.error(message)
def is_good_response(response: requests.Response) -> bool:
content_type = response.headers.get("Content-Type", "").lower()
return response.status_code == 200 and "html" in content_type
def simple_get(url: str, session: requests.Session) -> Optional[bytes]:
try:
with closing(
session.get(
url,
stream=True,
timeout=REQUEST_TIMEOUT_SECONDS,
)
) as response:
if is_good_response(response):
return response.content
log_error(
f"Request to {url} returned status {response.status_code} with content type "
f"{response.headers.get('Content-Type', 'unknown')}."
)
return None
except requests.RequestException as exc:
log_error(f"Error during request to {url}: {exc}")
return None
def get_soup(url: str, session: requests.Session) -> BeautifulSoup:
raw_html = simple_get(url, session)
if raw_html is None:
raise RuntimeError(f"Could not retrieve HTML content from {url}.")
return BeautifulSoup(raw_html, "html.parser")
def get_next_url(html: BeautifulSoup) -> Optional[str]:
link = html.find("link", {"rel": "next"})
if link is None:
return None
return link.get("href")
def get_chapter(html: BeautifulSoup, url: str) -> str:
title = html.find("h1", {"class": ENTRY_TITLE_CLASS})
if title is None:
raise RuntimeError(f"Could not find a chapter title on {url}.")
content = html.find("div", {"class": ENTRY_CONTENT_CLASS})
if content is None:
raise RuntimeError(f"Could not find the chapter body on {url}.")
paragraphs = content.find_all("p")
if not paragraphs:
raise RuntimeError(f"Could not find paragraph content on {url}.")
chapter_body = "".join(str(paragraph) for paragraph in paragraphs)
return f"{title}\n{chapter_body}\n"
def scrape() -> int:
validate_configuration()
current_url = STARTING_URL
visited_urls: Set[str] = set()
chapter_count = 0
output_path = Path(OUTPUT_FILE)
with requests.Session() as session:
session.headers.update({"User-Agent": "WordpressScraper/1.0"})
with output_path.open("w", encoding="utf-8") as output_handle:
while True:
if current_url in visited_urls:
raise RuntimeError(f"Detected a loop in the next-post chain at {current_url}.")
visited_urls.add(current_url)
LOGGER.info("Fetching %s", current_url)
html = get_soup(current_url, session)
output_handle.write(get_chapter(html, current_url))
chapter_count += 1
if current_url == ENDING_URL:
return chapter_count
next_url = get_next_url(html)
if not next_url:
raise RuntimeError(
"Could not find the next post before reaching ENDING_URL. "
f"Last successful URL: {current_url}"
)
current_url = next_url
def main() -> None:
configure_logging()
try:
chapter_count = scrape()
except Exception as exc: # noqa: BLE001
LOGGER.error("Scrape failed: %s", exc)
raise SystemExit(1) from exc
LOGGER.info("Done. Wrote %s chapter(s) to %s.", chapter_count, OUTPUT_FILE)
if __name__ == "__main__":
main()