ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [NodeJs] playwright example
    Back/NodeJS 2022. 12. 17. 19:55

    * playwright : headless 브라우저를 지원하는 라이브러리

     

     

    - chromium 을 통하여  http://httpbin.org/anything  접속한다.

    const playwright = require("playwright");
    
    (async () => {
        const browser = await playwright.chromium.launch();
        const context = await browser.newContext();
        const page = await context.newPage();
    
        await page.goto('http://httpbin.org/anything');
        console.log(await page.locator('pre').textContent());
    
        await browser.close();
    })();
    {
      "args": {}, 
      "data": "", 
      "files": {}, 
      "form": {}, 
      "headers": {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", 
        "Accept-Encoding": "gzip, deflate", 
        "Host": "httpbin.org", 
        "Proxy-Connection": "keep-alive", 
        "Referrer": "https://news.ycombinator.com/", 
        "Upgrade-Insecure-Requests": "1", 
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/109.0.5414.46 Safari/537.36", 
        "X-Amzn-Trace-Id": "Root=1-639d9b55-3c2bfecc2e12f67e244539f7"
      }, 
      "json": null, 
      "method": "GET", 
      "origin": "58.XXX.XXX.XXX", 
      "url": "http://httpbin.org/anything"
    }

     


     

    - request header 에 referrer , origin , User-Agent 을 변경한다.

     

    const playwright = require("playwright");
    
    (async () => {
        const browser = await playwright.chromium.launch({
            // origin 변경 free-proxy-list.net 참고
            proxy: { server: "http://111.251.181.239:80" },
        });
    
        const context = await browser.newContext();
        const page = await context.newPage();
    
        // Referrer , User-Agent 변경
        await page.setExtraHTTPHeaders({
            referrer : 'https://news.ycombinator.com/',
            'User-Agent' : 'Mozilla/5.0',
        });
    
        await page.goto('http://httpbin.org/anything');
        console.log(await page.locator('pre').textContent());
    
        await browser.close();
    
    })();
    {
      "args": {}, 
      "data": "", 
      "files": {}, 
      "form": {}, 
      "headers": {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", 
        "Accept-Encoding": "gzip, deflate", 
        "Host": "httpbin.org", 
        "Proxy-Connection": "keep-alive", 
        "Referrer": "https://news.ycombinator.com/", 
        "Upgrade-Insecure-Requests": "1", 
        "User-Agent": "Mozilla/5.0", 
        "X-Amzn-Trace-Id": "Root=1-639d9f0e-3ff984756619e1d5410411b0"
      }, 
      "json": null, 
      "method": "GET", 
      "origin": "111.251.181.239", 
      "url": "http://httpbin.org/anything"
    }
Designed by Tistory.