문서

문서

ActiCrawl을 사용하여 웹 스크래핑 워크플로를 자동화하는 방법을 알아보세요

설치

ActiCrawl은 인기 있는 프로그래밍 언어를 위한 공식 SDK를 제공하여 웹 스크래핑을 애플리케이션에 쉽게 통합할 수 있습니다.

공식 SDK

Node.js / JavaScript

npm을 통해 설치:

bash
npm install @acticrawl/sdk

또는 yarn 사용:

bash
yarn add @acticrawl/sdk

기본 사용법:

javascript
import { ActiCrawl } from '@acticrawl/sdk';

const client = new ActiCrawl({
  apiKey: 'YOUR_API_KEY'
});

const result = await client.scrape({
  url: 'https://example.com',
  format: 'markdown'
});

console.log(result.content);

Python

pip를 통해 설치:

bash
pip install acticrawl

기본 사용법:

python
from acticrawl import ActiCrawl

client = ActiCrawl(api_key='YOUR_API_KEY')

result = client.scrape(
    url='https://example.com',
    format='markdown'
)

print(result['content'])

Ruby

Gemfile에 추가:

ruby
gem 'acticrawl'

그 다음 실행:

bash
bundle install

기본 사용법:

ruby
require 'acticrawl'

client = ActiCrawl::Client.new(api_key: 'YOUR_API_KEY')

result = client.scrape(
  url: 'https://example.com',
  format: 'markdown'
)

puts result['content']

PHP

Composer를 통해 설치:

bash
composer require acticrawl/php-sdk

기본 사용법:

php
<?php
require 'vendor/autoload.php';

use ActiCrawl\Client;

$client = new Client('YOUR_API_KEY');

$result = $client->scrape([
    'url' => 'https://example.com',
    'format' => 'markdown'
]);

echo $result['content'];

Go

설치:

bash
go get github.com/acticrawl/go-sdk

기본 사용법:

go
package main

import (
    "fmt"
    "github.com/acticrawl/go-sdk"
)

func main() {
    client := acticrawl.NewClient("YOUR_API_KEY")

    result, err := client.Scrape(acticrawl.ScrapeOptions{
        URL:    "https://example.com",
        Format: "markdown",
    })

    if err != nil {
        panic(err)
    }

    fmt.Println(result.Content)
}

REST API

사용하는 언어에 공식 SDK가 없다면 REST API를 직접 사용할 수 있습니다:

bash
curl -X POST https://api.acticrawl.com/v1/scrape \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com",
    "format": "markdown"
  }'

환경 변수

보안을 위해 API 키를 환경 변수에 저장하는 것을 권장합니다:

Unix/Linux/macOS

bash
export ACTICRAWL_API_KEY="your_api_key_here"

Windows

cmd
set ACTICRAWL_API_KEY=your_api_key_here

.env 파일

프로젝트 루트에 .env 파일 생성:

env
ACTICRAWL_API_KEY=your_api_key_here

코드에서 사용:

javascript
// Node.js with dotenv
require('dotenv').config();

const client = new ActiCrawl({
  apiKey: process.env.ACTICRAWL_API_KEY
});
python
# Python with python-dotenv
from dotenv import load_dotenv
import os

load_dotenv()

client = ActiCrawl(api_key=os.getenv('ACTICRAWL_API_KEY'))

프레임워크 통합

Next.js

javascript
// pages/api/scrape.js
import { ActiCrawl } from '@acticrawl/sdk';

const client = new ActiCrawl({
  apiKey: process.env.ACTICRAWL_API_KEY
});

export default async function handler(req, res) {
  const { url } = req.body;

  try {
    const result = await client.scrape({
      url,
      format: 'markdown'
    });

    res.status(200).json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
}

Django

python
# views.py
from django.http import JsonResponse
from acticrawl import ActiCrawl
import os

client = ActiCrawl(api_key=os.environ.get('ACTICRAWL_API_KEY'))

def scrape_view(request):
    url = request.POST.get('url')

    try:
        result = client.scrape(url=url, format='markdown')
        return JsonResponse(result)
    except Exception as e:
        return JsonResponse({'error': str(e)}, status=500)

Laravel

php
// app/Http/Controllers/ScrapeController.php
namespace App\Http\Controllers;

use ActiCrawl\Client;
use Illuminate\Http\Request;

class ScrapeController extends Controller
{
    private $client;

    public function __construct()
    {
        $this->client = new Client(env('ACTICRAWL_API_KEY'));
    }

    public function scrape(Request $request)
    {
        $result = $this->client->scrape([
            'url' => $request->input('url'),
            'format' => 'markdown'
        ]);

        return response()->json($result);
    }
}

Docker

Docker를 사용하는 경우 API 키를 환경 변수로 전달할 수 있습니다:

dockerfile
FROM node:18-alpine

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

ENV ACTICRAWL_API_KEY=${ACTICRAWL_API_KEY}

CMD ["node", "index.js"]

실행:

bash
docker run -e ACTICRAWL_API_KEY=your_api_key_here your-image

문제 해결

일반적인 문제

  1. 인증 오류

    • API 키가 올바른지 확인
    • 키에 적절한 권한이 있는지 확인
    • Bearer 토큰 형식을 사용하고 있는지 확인
  2. 네트워크 오류

    • 인터넷 연결 확인
    • 방화벽 설정 확인
    • API 엔드포인트에 접근 가능한지 확인
  3. 요청 제한 오류

    • 플랜의 요청 제한 확인
    • 지수 백오프 구현
    • 플랜 업그레이드 고려

디버그 모드

상세한 로그를 보려면 디버그 모드를 활성화하세요:

javascript
const client = new ActiCrawl({
  apiKey: 'YOUR_API_KEY',
  debug: true
});

다음 단계