Cách trích xuất dữ liệu từ Web sử dụng GraphQL bằng Google Apps Script

Trong bài viết này, Thanh sẽ chia sẻ với các bạn cách trích xuất dữ liệu từ Website load dữ liệu qua GraphQL sử dụng Google Apps Script trong Google Sheets. Bài viết nằm trong Series 3 bài viết về chủ đề trích xuất dữ liệu từ GraphQL, các bạn có thể theo dõi 2 bài còn lại ở địa chỉ sau đây:

Lưu ý Bài viết này trình bày kiến thức nâng cao về Google Apps Script và cơ chế tải dữ liệu của trang Web, hãy đảm bảo bạn có kiến thức cơ bản để có thể hiểu và áp dụng cho trường hợp của bạn. Kiến thức về trích xuất dữ liệu từ nhiều nguồn online được Thanh trình bày hệ thống và cơ bản hơn trong hai khóa học sau đây, các bạn có thể tham khảo:

Mục tiêu trích xuất là dữ liệu trên trang Axie.zone. Kiến thức cơ bản về cơ chế tải dữ liệu của Web được trình bày trong bài viết đầu tiên trong Series, các bạn có thể theo dõi lại bài viết này. Sau đó, chúng ta có thể theo dõi video sau đây để học cách trích xuất dữ liệu từ Web sử dụng GraphQL bằng Google Apps Script:

YouTube video

Đoạn code sử dụng trong Video:

function requestData() {
  var myHeaders = {
    "authority": "axieinfinity.com",
    "sec-ch-ua": "\"Chromium\";v=\"94\", \"Google Chrome\";v=\"94\", \";Not A Brand\";v=\"99\"",
    "accept": "*/*",
    "content-type": "application/json",
    "sec-ch-ua-mobile": "?0",
    "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36",
    "sec-ch-ua-platform": "\"macOS\"",
    "origin": "https://axie.zone",
    "sec-fetch-site": "cross-site",
    "sec-fetch-mode": "cors",
    "sec-fetch-dest": "empty",
    "referer": "https://axie.zone/",
    "accept-language": "en-US,en;q=0.9,vi;q=0.8",
  }

  var raw = JSON.stringify({
    "operationName": "GetAxieBriefList",
    "query": "query GetAxieBriefList($auctionType: AuctionType, $criteria: AxieSearchCriteria, $from: Int, $sort: SortBy, $size: Int, $owner: String) { axies(auctionType: $auctionType, criteria: $criteria, from: $from, sort: $sort, size: $size, owner: $owner) {   total   results {     ...AxieBrief     __typename   }   __typename }       }       fragment AxieBrief on Axie { id name stage class breedCount image title genes battleInfo {   banned   __typename } auction {   currentPrice   currentPriceUSD   __typename } stats {   ...AxieStats   __typename } parts {   id   name   class   type   specialGenes   __typename } __typename       }            fragment AxieStats on AxieStats {        hp        speed        skill        morale __typename       }",
    "variables": {
      "auctionType": "Sale",
      "criteria": {
        "classes": [
          "Dusk"
        ],
        "parts": [
          "mouth-tiny-turtle",
          "mouth-tiny-carrot",
          "mouth-dango",
          "horn-lagging",
          "horn-laggingggggg",
          "back-snail-shell",
          "back-starry-shell",
          "tail-thorny-caterpillar",
          "tail-thorny-catterpilar"
        ],
        "hp": null,
        "speed": [
          46,
          61
        ],
        "skill": null,
        "morale": null,
        "breedCount": null,
        "pureness": [],
        "numMystic": [],
        "title": null,
        "region": null,
        "stages": [
          3,
          4
        ]
      },
      "from": 24,
      "size": 12,
      "sort": "PriceAsc",
      "owner": null
    }
  });

  var requestOptions = {
    method: 'POST',
    headers: myHeaders,
    payload: raw,
    redirect: 'follow'
  };

  var response = UrlFetchApp.fetch("https://axieinfinity.com/graphql-server-v2/graphql", requestOptions)
    
  Logger.log(response)

}

Khóa học mới xuất bản