Scrapers¶
scrapers ¶
Assembly: Fonky
Filename: scrapers.py
Author: Terry D. Eppler
Created: 05-31-2022
Last Modified By: Terry D. Eppler
Last Modified On: 05-01-2025
scrapers.py
Copyright © 2022 Terry Eppler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
You can contact me at: terryeppler@gmail.com or eppler.terry@epa.gov
Extractor ¶
Provide shared state for HTML extraction classes.
Purpose
Defines the minimal base state used by concrete scraper implementations that retrieve raw HTML, parse it with BeautifulSoup, and store extracted text. The class provides a common inspection surface for extraction-oriented subclasses without performing network or parsing work by itself.
Attributes:
| Name | Type | Description |
|---|---|---|
raw_html |
Optional[str]
|
Raw HTML captured for extraction. |
extracted_text |
Optional[str]
|
Extracted text generated from the source HTML. |
soup |
Optional[BeautifulSoup]
|
Parsed BeautifulSoup document tree. |
Source code in scrapers.py
WebExtractor ¶
Bases: Extractor
Fetch and extract selected structures from HTML pages.
Purpose
Provides synchronous HTML retrieval through requests and extraction helpers for common HTML
structures. The class stores request state, parser state, regular expressions, and headers so
individual scrape methods can request a page and return only the requested type of extracted
content.
Attributes:
| Name | Type | Description |
|---|---|---|
soup |
Optional[BeautifulSoup]
|
Parsed BeautifulSoup document tree. |
agents |
Optional[str]
|
User-agent string loaded from configuration. |
url |
Optional[str]
|
URL used for the active scrape request. |
html |
Optional[str]
|
Raw HTML text retained by the extractor. |
re_tag |
Optional[Pattern]
|
Compiled tag-removal regular expression. |
re_ws |
Optional[Pattern]
|
Compiled whitespace-normalization regular expression. |
response |
Optional[Response]
|
Most recent HTTP response. |
Source code in scrapers.py
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 | |
scrape ¶
Fetch a web page.
Purpose
Performs a synchronous HTTP GET request for the supplied URL, stores the response and timeout
state, validates HTTP success, and returns the canonical Fonky Result wrapper for
downstream inspection or serialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Absolute URL to fetch. |
required |
time
|
int
|
Request timeout in seconds. |
10
|
Returns:
| Type | Description |
|---|---|
Result | None
|
Result wrapper for the successful HTTP response. |
Raises:
| Type | Description |
|---|---|
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in scrapers.py
html_to_text ¶
Convert HTML to plain text.
Purpose
Removes script and style blocks, inserts spacing around common block-level tags, strips remaining HTML markup, and normalizes whitespace into compact readable text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html
|
str
|
Raw HTML string to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Plain text extracted from the supplied HTML. |
Raises:
| Type | Description |
|---|---|
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in scrapers.py
scrape_paragraphs ¶
Extract paragraph text.
Purpose
Fetches the target HTML document, parses it with BeautifulSoup, extracts readable text from
all p elements, and returns only non-empty paragraph strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Fully qualified URI of the target HTML document. |
required |
Returns:
| Type | Description |
|---|---|
List[str] | None
|
Cleaned paragraph text entries. |
Raises:
| Type | Description |
|---|---|
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in scrapers.py
scrape_lists ¶
Extract list item text.
Purpose
Fetches the target HTML document, parses it with BeautifulSoup, extracts readable text from
all li elements, and returns only non-empty list item strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Fully qualified URI of the target HTML page. |
required |
Returns:
| Type | Description |
|---|---|
List[str] | None
|
Clean list item text segments. |
Raises:
| Type | Description |
|---|---|
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in scrapers.py
scrape_tables ¶
Extract table cell text.
Purpose
Fetches the target HTML document, parses all table structures, and returns a flattened
list of readable text from td and th cells.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Fully qualified URI of the target HTML document. |
required |
Returns:
| Type | Description |
|---|---|
List[str] | None
|
Table cell values extracted from all table rows. |
Raises:
| Type | Description |
|---|---|
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in scrapers.py
scrape_articles ¶
Extract article text.
Purpose
Fetches the target HTML page, parses it with BeautifulSoup, extracts consolidated readable
text from each article element, and returns only non-empty article blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Fully qualified URI of the target HTML page. |
required |
Returns:
| Type | Description |
|---|---|
List[str] | None
|
Article-level text blocks. |
Raises:
| Type | Description |
|---|---|
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in scrapers.py
scrape_headings ¶
Extract heading text.
Purpose
Fetches the target HTML document, parses it with BeautifulSoup, extracts readable text from
heading tags h1 through h6, and returns only non-empty headings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Fully qualified URI of the target HTML document. |
required |
Returns:
| Type | Description |
|---|---|
List[str] | None
|
Clean heading strings. |
Raises:
| Type | Description |
|---|---|
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in scrapers.py
scrape_divisions ¶
Extract division text.
Purpose
Fetches the target HTML document, parses it with BeautifulSoup, extracts readable text from
div elements, and returns only non-empty division text blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Fully qualified URI of the target HTML document. |
required |
Returns:
| Type | Description |
|---|---|
List[str] | None
|
Clean division text blocks. |
Raises:
| Type | Description |
|---|---|
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in scrapers.py
scrape_sections ¶
Extract section text.
Purpose
Fetches the target HTML document, parses it with BeautifulSoup, extracts readable text from
section elements, and returns only non-empty section text blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Fully qualified URI of the target HTML document. |
required |
Returns:
| Type | Description |
|---|---|
List[str] | None
|
Clean section text blocks. |
Raises:
| Type | Description |
|---|---|
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in scrapers.py
scrape_blockquotes ¶
Extract blockquote text.
Purpose
Fetches the target HTML document, parses it with BeautifulSoup, extracts readable text from
blockquote elements, and returns only non-empty quoted text entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Fully qualified URI of the target HTML document. |
required |
Returns:
| Type | Description |
|---|---|
List[str] | None
|
Cleaned blockquote text entries. |
Raises:
| Type | Description |
|---|---|
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in scrapers.py
scrape_hyperlinks ¶
Extract hyperlinks from an HTML page.
Purpose
Fetches the target HTML document, parses it with BeautifulSoup, extracts href values from
anchor tags, and returns only populated hyperlink values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Fully qualified URI of the target HTML page. |
required |
Returns:
| Type | Description |
|---|---|
List[str] | None
|
Hyperlink paths or URLs extracted from anchor tags. |
Raises:
| Type | Description |
|---|---|
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in scrapers.py
scrape_images ¶
Extract image references.
Purpose
Fetches the target HTML document, parses it with BeautifulSoup, extracts src values from
image tags, and returns only populated image references.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Fully qualified URI of the target HTML page. |
required |
Returns:
| Type | Description |
|---|---|
List[str] | None
|
Image source values extracted from image tags. |
Raises:
| Type | Description |
|---|---|
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in scrapers.py
create_schema ¶
create_schema(
function: str,
tool: str,
description: str,
parameters: dict,
required: list[str],
) -> Dict[str, str] | None
Create a dynamic tool schema.
Purpose
Constructs an OpenAI-style function tool schema from the supplied function name, service name, description, parameter schema, and required field list. The method validates required inputs and preserves the caller-provided JSON-schema fragments for individual parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
str
|
Function name exposed to the model or tool caller. |
required |
tool
|
str
|
Underlying system or service wrapped by the function. |
required |
description
|
str
|
Description of what the function does. |
required |
parameters
|
dict
|
JSON-schema property definitions keyed by parameter name. |
required |
required
|
list[str]
|
Required parameter names. When |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, str] | None
|
JSON-compatible dictionary defining the tool schema. |
Raises:
| Type | Description |
|---|---|
ValueError
|
Raised when |
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in scrapers.py
throw_if ¶
Validate a required scraper argument.
Purpose
Validates that a required scraper argument is present and non-empty before network or parsing
work begins. The guard rejects None, blank strings, and empty container values with an
argument-specific ValueError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Argument name included in the validation error message. |
required |
value
|
object
|
Candidate value checked for |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
Validation succeeds silently; invalid values raise |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a required value is missing, blank, or outside the supported range. |