scrapingbee node
v1.7.5
ScrapingBee는 헤드리스 브라우저를 처리하고 프록시를 회전시키는 웹 스크래핑 API입니다. Node SDK를 사용하면 ScrapingBee의 API와 더 쉽게 상호작용할 수 있습니다.
npm을 사용하여 ScrapingBee Node SDK를 설치할 수 있습니다.
npm install scrapingbee
ScrapingBee Node SDK는 axios 라이브러리를 둘러싼 래퍼입니다. ScrapingBee는 GET 및 POST 요청을 지원합니다.
시작하려면 ScrapingBee에 가입하여 API 키와 무료 크레딧을 받으세요.
const scrapingbee = require ( 'scrapingbee' ) ;
async function get ( url ) {
var client = new scrapingbee . ScrapingBeeClient ( 'REPLACE-WITH-YOUR-API-KEY' ) ;
var response = await client . get ( {
// The URL you want to scrape
url : url ,
params : {
// Block ads on the page you want to scrape
block_ads : false ,
// Block images and CSS on the page you want to scrape
block_resources : true ,
// Premium proxy geolocation
country_code : '' ,
// Control the device the request will be sent from
device : 'desktop' ,
// Use some data extraction rules
extract_rules : { title : 'h1' } ,
// Wrap response in JSON
json_response : false ,
// JavaScript scenario to execute (clicking on button, scrolling ...)
js_scenario : {
instructions : [
{ wait_for : '#slow_button' } ,
{ click : '#slow_button' } ,
{ scroll_x : 1000 } ,
{ wait : 1000 } ,
{ scroll_x : 1000 } ,
{ wait : 1000 } ,
] ,
} ,
// Use premium proxies to bypass difficult to scrape websites (10-25 credits/request)
premium_proxy : false ,
// Execute JavaScript code with a Headless Browser (5 credits/request)
render_js : true ,
// Return the original HTML before the JavaScript rendering
return_page_source : false ,
// Return page screenshot as a png image
screenshot : false ,
// Take a full page screenshot without the window limitation
screenshot_full_page : false ,
// Transparently return the same HTTP code of the page requested.
transparent_status_code : false ,
// Wait, in miliseconds, before returning the response
wait : 0 ,
// Wait for CSS selector before returning the response, ex ".title"
wait_for : '' ,
// Set the browser window width in pixel
window_width : 1920 ,
// Set the browser window height in pixel
window_height : 1080 ,
} ,
headers : {
// Forward custom headers to the target website
key : 'value' ,
} ,
cookies : {
// Forward custom cookies to the target website
name : 'value' ,
} ,
// `timeout` specifies the number of milliseconds before the request times out.
// If the request takes longer than `timeout`, the request will be aborted.
timeout : 10000 , // here 10sec, default is `0` (no timeout)
} ) ;
var decoder = new TextDecoder ( ) ;
var text = decoder . decode ( response . data ) ;
console . log ( text ) ;
}
get ( 'https://httpbin-scrapingbee.cleverapps.io/html' ) . catch ( ( e ) => console . log ( 'A problem occurs : ' + e . message ) ) ;
/* -- output
<!DOCTYPE html><html lang="en"><head>...
*/
ScrapingBee는 다양한 매개변수를 사용하여 JavaScript를 렌더링하고, 사용자 정의 JavaScript 스크립트를 실행하고, 특정 지리적 위치의 프리미엄 프록시를 사용하는 등의 작업을 수행합니다.
ScrapingBee 문서에서 지원되는 모든 매개변수를 찾을 수 있습니다.
요청 라이브러리에서 일반적으로 하는 것처럼 사용자 정의 쿠키와 헤더를 보낼 수 있습니다.
다음은 ScrapingBee 블로그의 스크린샷을 모바일 해상도로 검색하고 저장하는 방법에 대한 작은 예입니다.
const fs = require ( 'fs' ) ;
const scrapingbee = require ( 'scrapingbee' ) ;
async function screenshot ( url , path ) {
var client = new scrapingbee . ScrapingBeeClient ( 'REPLACE-WITH-YOUR-API-KEY' ) ;
var response = await client . get ( {
url : url ,
params : {
screenshot : true , // Take a screenshot
screenshot_full_page : true , // Specify that we need the full height
window_width : 375 , // Specify a mobile width in pixel
} ,
} ) ;
fs . writeFileSync ( path , response . data ) ;
}
screenshot ( 'https://httpbin-scrapingbee.cleverapps.io/html' , './httpbin.png' ) . catch ( ( e ) =>
console . log ( 'A problem occurs : ' + e . message )
) ;
클라이언트에는 5XX 응답에 대한 재시도 메커니즘이 포함되어 있습니다.
const spb = require ( 'scrapingbee' ) ;
async function get ( url ) {
let client = new spb . ScrapingBeeClient ( 'REPLACE-WITH-YOUR-API-KEY' ) ;
let resp = await client . get ( { url : url , params : { render_js : false } , retries : 5 } ) ;
let decoder = new TextDecoder ( ) ;
let text = decoder . decode ( resp . data ) ;
console . log ( text ) ;
}
get ( 'https://httpbin-scrapingbee.cleverapps.io/html' ) . catch ( ( e ) => console . log ( 'A problem occured: ' + e . message ) ) ;