windlass.providers.loaders.web¶
web
¶
HTML, web-page and YouTube loaders.
The HTML loader works with no dependencies (falling back to a regex-based tag stripper) but does a considerably better job with BeautifulSoup installed. The web loader adds HTTP fetching, and the YouTube loader pulls transcripts.
Install the optional pieces with::
pip install "windlass[loaders]"
Example
HTMLLoader().load(b"
Hi
")[0].content 'Hi'
HTMLLoader
¶
HTMLLoader(
*,
selector: str | None = None,
remove_tags: tuple[str, ...] = _NOISE_TAGS,
keep_links: bool = False,
**config: Any
)
Bases: Loader
Extracts readable text from HTML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str | None
|
CSS selector for the content region. When omitted the loader
tries :data: |
None
|
remove_tags
|
tuple[str, ...]
|
Elements to strip entirely before extraction. |
_NOISE_TAGS
|
keep_links
|
bool
|
Render links as |
False
|
**config
|
Any
|
Forwarded to :class: |
{}
|
Note
Works without beautifulsoup4 using a regex stripper, which is fine
for well-formed markup. Install windlass[loaders] for real
parsing, boilerplate removal and CSS selectors.
Source code in src\windlass\providers\loaders\web.py
aload_source
async
¶
Extract text from one HTML document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
SourceLike
|
A path or a bytes payload. |
required |
Returns:
| Type | Description |
|---|---|
list[Document]
|
A single-element list, empty when no text could be extracted. |
Source code in src\windlass\providers\loaders\web.py
extract
¶
Turn HTML into text plus metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html
|
str
|
The HTML source. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A |
dict[str, Any]
|
|
Example
text, meta = HTMLLoader().extract("
T Body
") meta["title"], text ('T', 'Body')
Source code in src\windlass\providers\loaders\web.py
WebLoader
¶
WebLoader(
*,
headers: dict[str, str] | None = None,
follow_redirects: bool = True,
timeout: float | None = None,
max_bytes: int = 10000000,
**config: Any
)
Bases: HTMLLoader
Fetches URLs over HTTP and extracts their text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
headers
|
dict[str, str] | None
|
Extra request headers. A browser-like |
None
|
follow_redirects
|
bool
|
Follow 3xx responses. |
True
|
timeout
|
float | None
|
Per-request timeout in seconds. |
None
|
max_bytes
|
int
|
Refuse responses larger than this, so one enormous page cannot exhaust memory. |
10000000
|
**config
|
Any
|
Forwarded to :class: |
{}
|
Raises:
| Type | Description |
|---|---|
IngestionError
|
On a network failure or a non-2xx response. |
Performance
URLs are fetched concurrently by the base class, bounded by the global
max_concurrency setting. Be considerate of the sites you crawl.
Source code in src\windlass\providers\loaders\web.py
can_handle
classmethod
¶
Return whether source looks like an HTTP(S) URL.
aload_source
async
¶
Fetch a URL and extract its text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
SourceLike
|
The URL to fetch. |
required |
Returns:
| Type | Description |
|---|---|
list[Document]
|
A single-element list, empty when the page had no extractable text. |
Raises:
| Type | Description |
|---|---|
IngestionError
|
On a network error or an HTTP error status. |
Source code in src\windlass\providers\loaders\web.py
acrawl
async
¶
Fetch several URLs concurrently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
urls
|
list[str]
|
The URLs to fetch. |
required |
concurrency
|
int | None
|
Maximum simultaneous requests. |
None
|
Returns:
| Type | Description |
|---|---|
list[Document]
|
Every document that loaded successfully; failures are logged and |
list[Document]
|
skipped. |
Source code in src\windlass\providers\loaders\web.py
YouTubeLoader
¶
YouTubeLoader(
*,
languages: tuple[str, ...] = ("en",),
chunk_by_time: bool = False,
segment_seconds: int = 300,
**config: Any
)
Bases: Loader
Loads transcripts for YouTube videos.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
languages
|
tuple[str, ...]
|
Preferred transcript languages, in order of preference. |
('en',)
|
chunk_by_time
|
bool
|
Emit one document per |
False
|
segment_seconds
|
int
|
Window length when |
300
|
**config
|
Any
|
Forwarded to :class: |
{}
|
Raises:
| Type | Description |
|---|---|
MissingDependencyError
|
When |
IngestionError
|
When the video has no accessible transcript. |
Note
Only videos with captions (auto-generated or human) can be loaded. There
is no audio transcription here — use the audio loader for that.
Source code in src\windlass\providers\loaders\web.py
can_handle
classmethod
¶
Return whether source is a YouTube URL or a bare video id.
Source code in src\windlass\providers\loaders\web.py
aload_source
async
¶
Fetch one video's transcript.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
SourceLike
|
A YouTube URL or an 11-character video id. |
required |
Returns:
| Type | Description |
|---|---|
list[Document]
|
One document per time segment, or one for the whole transcript. |
Raises:
| Type | Description |
|---|---|
IngestionError
|
When no transcript is available. |
Source code in src\windlass\providers\loaders\web.py
extract_video_id
¶
Pull the 11-character video id out of a YouTube URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
A YouTube URL in any of its common shapes, or a bare id. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The video id. |
Raises:
| Type | Description |
|---|---|
IngestionError
|
When no id can be found. |
Example
extract_video_id("https://youtu.be/dQw4w9WgXcQ") 'dQw4w9WgXcQ' extract_video_id("dQw4w9WgXcQ") 'dQw4w9WgXcQ'