fix: Update pdf and screenshot usage documentation. ref #1230
This commit is contained in:
@@ -5359,29 +5359,38 @@ Sometimes you need a visual record of a page or a PDF “printout.” Crawl4AI c
|
|||||||
```python
|
```python
|
||||||
import os, asyncio
|
import os, asyncio
|
||||||
from base64 import b64decode
|
from base64 import b64decode
|
||||||
from crawl4ai import AsyncWebCrawler, CacheMode
|
from crawl4ai import AsyncWebCrawler, CacheMode, CrawlerRunConfig
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
run_config = CrawlerRunConfig(
|
||||||
|
cache_mode=CacheMode.BYPASS,
|
||||||
|
screenshot=True,
|
||||||
|
pdf=True
|
||||||
|
)
|
||||||
|
|
||||||
async with AsyncWebCrawler() as crawler:
|
async with AsyncWebCrawler() as crawler:
|
||||||
result = await crawler.arun(
|
result = await crawler.arun(
|
||||||
url="https://en.wikipedia.org/wiki/List_of_common_misconceptions",
|
url="https://en.wikipedia.org/wiki/List_of_common_misconceptions",
|
||||||
cache_mode=CacheMode.BYPASS,
|
config=run_config
|
||||||
pdf=True,
|
|
||||||
screenshot=True
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if result.success:
|
if result.success:
|
||||||
# Save screenshot
|
print(f"Screenshot data present: {result.screenshot is not None}")
|
||||||
|
print(f"PDF data present: {result.pdf is not None}")
|
||||||
|
|
||||||
if result.screenshot:
|
if result.screenshot:
|
||||||
|
print(f"[OK] Screenshot captured, size: {len(result.screenshot)} bytes")
|
||||||
with open("wikipedia_screenshot.png", "wb") as f:
|
with open("wikipedia_screenshot.png", "wb") as f:
|
||||||
f.write(b64decode(result.screenshot))
|
f.write(b64decode(result.screenshot))
|
||||||
|
else:
|
||||||
# Save PDF
|
print("[WARN] Screenshot data is None.")
|
||||||
|
|
||||||
if result.pdf:
|
if result.pdf:
|
||||||
|
print(f"[OK] PDF captured, size: {len(result.pdf)} bytes")
|
||||||
with open("wikipedia_page.pdf", "wb") as f:
|
with open("wikipedia_page.pdf", "wb") as f:
|
||||||
f.write(result.pdf)
|
f.write(result.pdf)
|
||||||
|
else:
|
||||||
print("[OK] PDF & screenshot captured.")
|
print("[WARN] PDF data is None.")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("[ERROR]", result.error_message)
|
print("[ERROR]", result.error_message)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user