Updated:

Tags: , , ,

Categories:

User-Agent 를 꼭 설정해두자

User-Agent를 따로 설정하지 않아도 정상적으로 Axios 요청이 진행되는 곳도 있지만, 그렇지 않은 곳(Bot으로 인식해서 차단하는 곳)도 있다. 그래서 이렇게 옵션에 User-Agent를 항상 추가해줘야 불필요한 오류를 막을 수 있을 것 같다.

const options = {
          headers: {
            'User-Agent':
              'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
          },
        };

const getData = await axios.get(`URL주소입력`, options);

Axios - 아직 해결하지 못한 문제

Axios.get 요청으로 어떤 링크의 robots.txt를 요청하고, 이게 만약 상태가 200(성공)이라면 또 다시 그 상태에서 humans.txt를 요청하게끔 만들고 싶었다. 그러니깐 하나의 링크로 항상 두번의 요청을 보내지 않고, 첫번째(robots.txt)가 성공일 시에만 추가로 한번 더 요청(humans.txt)을 하게해서 요청 횟수를 효율적이게 하려고 했다.

그런데 비동기 방식으로 하기위해 promise를 배열에 담아 Promise.allSettled 로 진행해보려고 했으나 문제가 발생했다. 이유는 아직 잘 모르겠으나.. 첫번째(robots.txt)와 두번째(humans.txt) 요청에 모두 200으로 응답을 받은 경우에만 리턴을 해주었다. 실패(404)하더라도 무시하고 받으려고 했으나 생각처럼 진행이 안되었다.

어찌저찌 결국에는 비동기로 여러개를 동시에 요청해서 진행하려했지만 포기하고 각각 await으로 하나씩 요청을 받고 진행하게끔 했다.


Docker 에서 Puppeteer 설치시 문제

Docker를 빌드하는 중에 Puppeteer에서 문제가 발생했다.

#9 24.69 [4/4] Building fresh packages...
#9 24.97 error /myfolder/node_modules/puppeteer: Command failed.
#9 24.97 Exit code: 1
#9 24.97 Command: node install.js
#9 24.97 Arguments: 
#9 24.97 Directory: /myfolder/node_modules/puppeteer
#9 24.97 Output:
#9 24.97 The chromium binary is not available for arm64.
#9 24.97 If you are on Ubuntu, you can install with: 
#9 24.97 
#9 24.97  sudo apt install chromium
#9 24.97 
#9 24.97 
#9 24.97  sudo apt install chromium-browser
#9 24.97 
#9 24.97 ERROR: Failed to set up Chromium r1011831! Set "PUPPETEER_SKIP_DOWNLOAD" env variable to skip download.
#9 24.97 Error
#9 24.97     at handleArm64 (/myfolder/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserFetcher.js:138:11)
#9 24.97     at BrowserFetcher.download (/myfolder/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserFetcher.js:283:13)
#9 24.97     at async downloadBrowser (/myfolder/node_modules/puppeteer/lib/cjs/puppeteer/node/install.js:65:5)
#9 24.97 info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

이런 에러메세지들이 나왔는데, 보니깐 크로미움 설치과정중에서 에러가 발생하는 것 같다. 해결방법을 찾아보니 대부분 도커가 아닌 로컬에서 직접 설치할 때 해결방법만 나와서 해맸는데, 결과적으로는 Dockerfile 안에 RUN yarn install 위에 줄에 ENV PUPPETEER_SKIP_DOWNLOAD=true 를 추가해주면 해결된다.
이미 사용중인 .env 파일안에 PUPPETEER_SKIP_DOWNLOAD="true" 를 추가해봤지만 인식하지 못했고 Dockerfile 에 추가하니 인식이 잘 되었다.

Leave a comment