No project description provided
Project description
Python Web Scraper
This script utilizes the Playwright library for scraping websites and generating a knowledgebase. It is capable of outputting the scraped data in multiple formats, including JSON, plain text, and PDF.
Main Functions
scrape_it(config: Config)
Parameters
config: An object containing URLs to scrape, output file names, file types, and a limit on pages to crawl.
Workflow
- Launch Chromium Browser: Uses Playwright to start a new browser instance.
- URL Iteration: For each URL in the
Configobject:- Sitemap Processing:
- Navigate to
sitemap.xml. - Raise
NoSitemapErrorif not found, else extract URLs.
- Navigate to
- Page Processing: For each URL in the sitemap:
- Stop if
max_pages_to_crawlis reached. - Navigate to the URL and extract the page content.
- Create a
WebPageobject with URL and content. - Clean content and add to
Knowledgebaseobject.
- Stop if
- Sitemap Processing:
- Output Generation:
- Calls
write_to_file(knowledgebase: Knowledgebase, config: Config)to generate outputs. - Supports multiple output formats as specified in
config(JSON, TXT, PDF).
- Calls
write_to_file(knowledgebase: Knowledgebase, config: Config)
Purpose
Writes the scraped data from Knowledgebase to files in the specified formats.
Functionality
- Directory Creation: Ensures the directory for storing files exists.
- File Generation:
- JSON Output: Stores the
Knowledgebaseobject in a JSON file. - Text Output: Creates a text file with all the page contents.
- PDF Output: Generates a PDF file with the content of each webpage.
- JSON Output: Stores the
Unique Filenames
- Uses
get_output_filename(file_name: str, file_type: str)to ensure unique file names for each output file.
string_to_pdf(knowledgebase: Knowledgebase, file_name)
Purpose
Converts the content of webpages in a Knowledgebase to a PDF file.
Functionality
- Handles UTF-8 encoding and falls back to DejaVu font if necessary.
- Generates a PDF with each page's URL and content.
Error Handling
- The script raises a
NoSitemapErrorif a sitemap.xml file is not found for a given URL. - Errors and statuses are printed to the console for debugging and monitoring.
Requirements
- Playwright for Python
- FPDF library for PDF generation
- Pydantic for data model validation
Usage
poetry add tedfulk-kb-pycrawler
playwright install
import asyncio
from tedfulk_kb_pycrawler import scrape_it, Config
config = Config(
urls=[
"https://jxnl.github.io/instructor/",
],
output_file_name=["instructor"],
output_file_type=["pdf"],
max_pages_to_crawl=100,
)
asyncio.run(scrape_it(config))
Activate your virtual environment:
poetry shell
Run the script:
python main.py
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tedfulk_kb_pycrawler-0.1.6.tar.gz.
File metadata
- Download URL: tedfulk_kb_pycrawler-0.1.6.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
199b24efe3e512e56b8e54f1e0f26300d475923a2198fdab46c1b0f97711f0c4
|
|
| MD5 |
be2dc970b27cee9c883187154cce204c
|
|
| BLAKE2b-256 |
e33cda94905aa98646239b5b25532d160f69ee738682b20e388da25ea37033fa
|
File details
Details for the file tedfulk_kb_pycrawler-0.1.6-py3-none-any.whl.
File metadata
- Download URL: tedfulk_kb_pycrawler-0.1.6-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
efd9fb3334d31e1891e8fbd8e1887d4e9c3f6303699f50784dd94647f16d28a6
|
|
| MD5 |
ddbe0dc0c7f47a35888f02b4694dcae1
|
|
| BLAKE2b-256 |
481e3f10bed5be929d4ff1aeedd5f3ccdab4767c7090c4e45016b9db3b15ae88
|