The Playwright API is a RESTful API developed with FastAPI and Playwright.
87
The Playwright API is a RESTful API developed with FastAPI and Playwright, designed for web content scraping. It allows you to submit a list of URLs and receive back the text extracted from the <h1>, <h2>, <h3>, and <p> tags of each page, formatted in Markdown. This project is containerized with Docker, simplifying deployment and usage.
<h1>, <h2>, <h3>) and paragraphs (<p>) from web pages.nionmaron/playwright-api:v0.0.1
To start the API using Docker, execute the following command:
docker run -d --name playwright-api -p 8000:8000 nionmaron/playwright-api:v0.0.1
-d: Runs the container in detached mode (background).--name playwright-api: Names the container for easy reference.-p 8000:8000: Maps port 8000 of the host to port 8000 of the container.After starting the container, the API will be accessible at:
http://localhost:8000
Access the interactive documentation generated by FastAPI at:
http://localhost:8000/docs
/scrapePOSTReceives a list of URLs and returns the content extracted from the <h1>, <h2>, <h3>, and <p> tags in Markdown format.
{
"urls": [
"https://www.example.com",
"https://www.anotherexample.com"
]
}
{
"https://www.example.com": "# Page Title\n\nParagraph content...\n## Subtitle\n\nMore content...\n### Smaller Subtitle\n\nEven more content...\n",
"https://www.anotherexample.com": "# Another Title\n\nParagraph content...\n"
}
curl -X POST "http://localhost:8000/scrape" \
-H "Content-Type: application/json" \
-d '{"urls": ["https://www.infomoney.com.br/"]}'
{
"https://www.infomoney.com.br/": "# Page Title\n\nParagraph content...\n## Subtitle\n\nMore content...\n### Smaller Subtitle\n\nEven more content...\n"
}
If you use R to interact with the API, here's an example script:
# Load necessary packages
library(httr)
library(jsonlite)
# Define the API URL and the URL you want to scrape
api_url <- "http://localhost:8000/scrape"
target_urls <- c("https://www.infomoney.com.br/") # Add more URLs as needed
# Build the JSON payload
payload <- list(urls = as.list(target_urls))
payload_json <- toJSON(payload, auto_unbox = TRUE)
# Send the POST request to the API
response <- POST(
url = api_url,
body = payload_json,
encode = "json",
content_type_json()
)
# Check if the request was successful
if (status_code(response) == 200) {
# Parse the response content
response_content <- content(response, as = "text", encoding = "UTF-8")
response_json <- fromJSON(response_content)
for (url in target_urls) {
markdown_text <- response_json[[url]]
if (grepl("^Error", markdown_text)) {
cat(sprintf("Error processing %s: %s\n", url, markdown_text))
} else {
# Display the Markdown text in the console
cat(sprintf("Markdown Text for %s:\n\n", url))
cat(markdown_text)
# Optional: Save the text to a Markdown file
safe_filename <- gsub("https?://", "", url)
safe_filename <- gsub("[^a-zA-Z0-9_-]", "_", safe_filename)
filename <- paste0(safe_filename, ".md")
writeLines(markdown_text, con = filename)
cat(sprintf("\n\nContent saved to '%s'.\n\n", filename))
}
}
} else {
# If the request failed, display an error message
cat("Error accessing the API. Status:", status_code(response), "\n")
cat("Error message:", content(response, as = "text", encoding = "UTF-8"), "\n")
}
If you wish to customize the API or adjust parameters, you can clone the repository and build the Docker image locally.
git clone https://github.com/nionmaron/playwright-api.git
cd playwright-api
docker build -t playwright-api:v0.0.1 .
docker run -d --name playwright-api -p 8000:8000 playwright-api:v0.0.1
To view the API logs in real-time, use the following command:
docker logs -f playwright-api
This allows you to monitor requests, errors, and other relevant information directly in the terminal.
This project is licensed under the MIT License.
Developed by Nion Maron.
For any questions or suggestions, feel free to reach out.
Content type
Image
Digest
sha256:8ea75246d…
Size
818.8 MB
Last updated
over 1 year ago
docker pull nionmaron/playwright-api:v.0.0.1