langchain-thecrawler 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain_thecrawler-0.1.0/LICENSE +21 -0
- langchain_thecrawler-0.1.0/PKG-INFO +77 -0
- langchain_thecrawler-0.1.0/README.md +53 -0
- langchain_thecrawler-0.1.0/langchain_thecrawler/__init__.py +7 -0
- langchain_thecrawler-0.1.0/langchain_thecrawler/document_loaders.py +128 -0
- langchain_thecrawler-0.1.0/pyproject.toml +38 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 manchittlab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: langchain-thecrawler
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: An integration package connecting TheCrawler and LangChain
|
|
5
|
+
License: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Author: manchittlab
|
|
8
|
+
Requires-Python: >=3.9,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Requires-Dist: langchain-core (>=0.3.15,<0.4.0)
|
|
18
|
+
Requires-Dist: requests (>=2.31,<3.0)
|
|
19
|
+
Project-URL: Homepage, https://www.miaibot.ai
|
|
20
|
+
Project-URL: Repository, https://github.com/manchittlab/langchain-thecrawler
|
|
21
|
+
Project-URL: Source Code, https://github.com/manchittlab/langchain-thecrawler
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# langchain-thecrawler
|
|
25
|
+
|
|
26
|
+
This package contains the LangChain integration with [TheCrawler](https://www.miaibot.ai) — a web-scraping and structured-extraction API that runs the extraction LLM on its own GPU, so AI extraction is included on every page with no per-call surcharge.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install -U langchain-thecrawler
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Set your API key (get one at [miaibot.ai](https://www.miaibot.ai)):
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
export THECRAWLER_API_KEY="mai_live_..."
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Document Loader
|
|
41
|
+
|
|
42
|
+
`TheCrawlerLoader` loads one or more URLs as LangChain `Document` objects with boilerplate-stripped markdown as `page_content` and rich page metadata.
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from langchain_thecrawler import TheCrawlerLoader
|
|
46
|
+
|
|
47
|
+
loader = TheCrawlerLoader(
|
|
48
|
+
["https://example.com"],
|
|
49
|
+
# api_key="mai_live_...", # or set THECRAWLER_API_KEY
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
docs = loader.load() # list[Document]
|
|
53
|
+
# or stream:
|
|
54
|
+
for doc in loader.lazy_load():
|
|
55
|
+
print(doc.metadata["url"], len(doc.page_content))
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
PDF and DOCX URLs are handled server-side. A per-page failure does **not** raise — failed pages come back as a `Document` with empty `page_content` and `metadata["status"] == "error"` plus a structured `error_type`, so you can branch on it:
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
ok = [d for d in docs if d.metadata.get("status") != "error"]
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Options
|
|
65
|
+
|
|
66
|
+
| Arg | Description |
|
|
67
|
+
| --- | --- |
|
|
68
|
+
| `urls` | A URL string or list of URLs (required) |
|
|
69
|
+
| `api_key` | TheCrawler key; falls back to `THECRAWLER_API_KEY` |
|
|
70
|
+
| `api_url` | API base URL (default `https://www.miaibot.ai/api/v1`) |
|
|
71
|
+
| `params` | Extra options merged into the crawl request (e.g. `{"usePlaywright": True}`) |
|
|
72
|
+
| `timeout` | Per-request HTTP timeout in seconds (default 120) |
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
MIT
|
|
77
|
+
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# langchain-thecrawler
|
|
2
|
+
|
|
3
|
+
This package contains the LangChain integration with [TheCrawler](https://www.miaibot.ai) — a web-scraping and structured-extraction API that runs the extraction LLM on its own GPU, so AI extraction is included on every page with no per-call surcharge.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install -U langchain-thecrawler
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Set your API key (get one at [miaibot.ai](https://www.miaibot.ai)):
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
export THECRAWLER_API_KEY="mai_live_..."
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Document Loader
|
|
18
|
+
|
|
19
|
+
`TheCrawlerLoader` loads one or more URLs as LangChain `Document` objects with boilerplate-stripped markdown as `page_content` and rich page metadata.
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from langchain_thecrawler import TheCrawlerLoader
|
|
23
|
+
|
|
24
|
+
loader = TheCrawlerLoader(
|
|
25
|
+
["https://example.com"],
|
|
26
|
+
# api_key="mai_live_...", # or set THECRAWLER_API_KEY
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
docs = loader.load() # list[Document]
|
|
30
|
+
# or stream:
|
|
31
|
+
for doc in loader.lazy_load():
|
|
32
|
+
print(doc.metadata["url"], len(doc.page_content))
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
PDF and DOCX URLs are handled server-side. A per-page failure does **not** raise — failed pages come back as a `Document` with empty `page_content` and `metadata["status"] == "error"` plus a structured `error_type`, so you can branch on it:
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
ok = [d for d in docs if d.metadata.get("status") != "error"]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Options
|
|
42
|
+
|
|
43
|
+
| Arg | Description |
|
|
44
|
+
| --- | --- |
|
|
45
|
+
| `urls` | A URL string or list of URLs (required) |
|
|
46
|
+
| `api_key` | TheCrawler key; falls back to `THECRAWLER_API_KEY` |
|
|
47
|
+
| `api_url` | API base URL (default `https://www.miaibot.ai/api/v1`) |
|
|
48
|
+
| `params` | Extra options merged into the crawl request (e.g. `{"usePlaywright": True}`) |
|
|
49
|
+
| `timeout` | Per-request HTTP timeout in seconds (default 120) |
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
|
|
53
|
+
MIT
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""TheCrawler document loader for LangChain."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from typing import Any, Dict, Iterator, List, Optional, Union
|
|
7
|
+
|
|
8
|
+
import requests
|
|
9
|
+
from langchain_core.document_loaders import BaseLoader
|
|
10
|
+
from langchain_core.documents import Document
|
|
11
|
+
|
|
12
|
+
_DEFAULT_API_URL = "https://www.miaibot.ai/api/v1"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TheCrawlerLoader(BaseLoader):
|
|
16
|
+
"""Load web pages as LangChain ``Document`` objects via the TheCrawler API.
|
|
17
|
+
|
|
18
|
+
TheCrawler returns boilerplate-stripped markdown plus rich page metadata
|
|
19
|
+
(title, description, status code, structured error type, ...) for each URL.
|
|
20
|
+
PDF and DOCX URLs are handled server-side. A per-page failure does **not**
|
|
21
|
+
raise: failed pages are yielded as ``Document`` objects with empty
|
|
22
|
+
``page_content`` and ``metadata["status"] == "error"`` (plus a structured
|
|
23
|
+
``error_type``), so caller code can branch on it instead of catching.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
urls: A single absolute URL or a list of URLs to load.
|
|
27
|
+
api_key: TheCrawler API key (``mai_live_...``). Falls back to the
|
|
28
|
+
``THECRAWLER_API_KEY`` environment variable.
|
|
29
|
+
api_url: Base URL of the API. Defaults to
|
|
30
|
+
``https://www.miaibot.ai/api/v1``. Override to point at a
|
|
31
|
+
self-hosted instance.
|
|
32
|
+
params: Extra options merged into the ``/crawl`` request body (e.g.
|
|
33
|
+
``{"usePlaywright": True, "requestTimeoutSecs": 60}``). See the
|
|
34
|
+
TheCrawler docs for the full option list.
|
|
35
|
+
timeout: Per-request HTTP timeout in seconds. Default 120.
|
|
36
|
+
|
|
37
|
+
Example:
|
|
38
|
+
.. code-block:: python
|
|
39
|
+
|
|
40
|
+
from langchain_thecrawler import TheCrawlerLoader
|
|
41
|
+
|
|
42
|
+
loader = TheCrawlerLoader(
|
|
43
|
+
["https://example.com"], api_key="mai_live_..."
|
|
44
|
+
)
|
|
45
|
+
docs = loader.load()
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
def __init__(
|
|
49
|
+
self,
|
|
50
|
+
urls: Union[str, List[str]],
|
|
51
|
+
*,
|
|
52
|
+
api_key: Optional[str] = None,
|
|
53
|
+
api_url: Optional[str] = None,
|
|
54
|
+
params: Optional[Dict[str, Any]] = None,
|
|
55
|
+
timeout: int = 120,
|
|
56
|
+
) -> None:
|
|
57
|
+
if isinstance(urls, str):
|
|
58
|
+
urls = [urls]
|
|
59
|
+
if not urls:
|
|
60
|
+
raise ValueError("`urls` must be a non-empty URL or list of URLs.")
|
|
61
|
+
|
|
62
|
+
resolved_key = api_key or os.getenv("THECRAWLER_API_KEY", "")
|
|
63
|
+
if not resolved_key:
|
|
64
|
+
raise ValueError(
|
|
65
|
+
"TheCrawler API key is required. Pass api_key=... or set the "
|
|
66
|
+
"THECRAWLER_API_KEY environment variable."
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
self.urls: List[str] = list(urls)
|
|
70
|
+
self.api_key: str = resolved_key
|
|
71
|
+
self.api_url: str = (api_url or _DEFAULT_API_URL).rstrip("/")
|
|
72
|
+
self.params: Dict[str, Any] = params or {}
|
|
73
|
+
self.timeout: int = timeout
|
|
74
|
+
|
|
75
|
+
def _post_crawl(self, body: Dict[str, Any]) -> Dict[str, Any]:
|
|
76
|
+
headers = {
|
|
77
|
+
"Authorization": f"Bearer {self.api_key}",
|
|
78
|
+
"Content-Type": "application/json",
|
|
79
|
+
}
|
|
80
|
+
response = requests.post(
|
|
81
|
+
f"{self.api_url}/crawl",
|
|
82
|
+
headers=headers,
|
|
83
|
+
json=body,
|
|
84
|
+
timeout=self.timeout,
|
|
85
|
+
)
|
|
86
|
+
response.raise_for_status()
|
|
87
|
+
return response.json()
|
|
88
|
+
|
|
89
|
+
@staticmethod
|
|
90
|
+
def _page_to_document(page: Dict[str, Any]) -> Document:
|
|
91
|
+
text = page.get("markdown") or page.get("text") or ""
|
|
92
|
+
metadata: Dict[str, Any] = {
|
|
93
|
+
"source": "thecrawler",
|
|
94
|
+
"url": page.get("url", ""),
|
|
95
|
+
"title": page.get("title"),
|
|
96
|
+
"description": page.get("description"),
|
|
97
|
+
"language": page.get("language"),
|
|
98
|
+
"canonical_url": page.get("canonicalUrl"),
|
|
99
|
+
"status": page.get("status"),
|
|
100
|
+
"status_code": page.get("statusCode"),
|
|
101
|
+
"content_type": page.get("contentType"),
|
|
102
|
+
"response_time_ms": page.get("responseTimeMs"),
|
|
103
|
+
"scraped_at": page.get("scrapedAt"),
|
|
104
|
+
"from_cache": page.get("fromCache"),
|
|
105
|
+
}
|
|
106
|
+
if page.get("status") == "error":
|
|
107
|
+
metadata["error"] = page.get("error")
|
|
108
|
+
metadata["error_type"] = page.get("errorType")
|
|
109
|
+
metadata["error_retryable"] = page.get("errorRetryable")
|
|
110
|
+
|
|
111
|
+
metadata = {k: v for k, v in metadata.items() if v is not None}
|
|
112
|
+
return Document(page_content=text, metadata=metadata)
|
|
113
|
+
|
|
114
|
+
def lazy_load(self) -> Iterator[Document]:
|
|
115
|
+
"""Crawl the configured URLs and yield one ``Document`` per page."""
|
|
116
|
+
body: Dict[str, Any] = {
|
|
117
|
+
"urls": list(self.urls),
|
|
118
|
+
"extractMarkdown": True,
|
|
119
|
+
"stripBoilerplate": True,
|
|
120
|
+
}
|
|
121
|
+
# User-supplied params override defaults.
|
|
122
|
+
if self.params:
|
|
123
|
+
body.update(self.params)
|
|
124
|
+
|
|
125
|
+
result = self._post_crawl(body)
|
|
126
|
+
pages = result.get("pages", []) if isinstance(result, dict) else []
|
|
127
|
+
for page in pages:
|
|
128
|
+
yield self._page_to_document(page)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "langchain-thecrawler"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "An integration package connecting TheCrawler and LangChain"
|
|
5
|
+
authors = ["manchittlab"]
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
repository = "https://github.com/manchittlab/langchain-thecrawler"
|
|
9
|
+
packages = [{ include = "langchain_thecrawler" }]
|
|
10
|
+
|
|
11
|
+
[tool.poetry.urls]
|
|
12
|
+
"Source Code" = "https://github.com/manchittlab/langchain-thecrawler"
|
|
13
|
+
"Homepage" = "https://www.miaibot.ai"
|
|
14
|
+
|
|
15
|
+
[tool.poetry.dependencies]
|
|
16
|
+
python = ">=3.9,<4.0"
|
|
17
|
+
langchain-core = "^0.3.15"
|
|
18
|
+
requests = "^2.31"
|
|
19
|
+
|
|
20
|
+
[tool.poetry.group.test.dependencies]
|
|
21
|
+
pytest = "^8.0"
|
|
22
|
+
pytest-asyncio = "^0.23"
|
|
23
|
+
pytest-socket = "^0.7"
|
|
24
|
+
pytest-mock = "^3.12"
|
|
25
|
+
langchain-tests = "^0.3.5"
|
|
26
|
+
|
|
27
|
+
[tool.poetry.group.lint.dependencies]
|
|
28
|
+
ruff = "^0.5"
|
|
29
|
+
|
|
30
|
+
[tool.ruff.lint]
|
|
31
|
+
select = ["E", "F", "I", "W"]
|
|
32
|
+
|
|
33
|
+
[tool.pytest.ini_options]
|
|
34
|
+
asyncio_mode = "auto"
|
|
35
|
+
|
|
36
|
+
[build-system]
|
|
37
|
+
requires = ["poetry-core>=1.0.0"]
|
|
38
|
+
build-backend = "poetry.core.masonry.api"
|