Bài viết này hướng dẫn bạn cách trích xuất dữ liệu từ website sử dụng GraphQL với Google Apps Script trong Google Sheets. Nội dung này thuộc chuỗi bài viết về trích xuất dữ liệu từ GraphQL. Bài viết tập trung vào kiến thức nâng cao về Google Apps Script và cơ chế tải dữ liệu web. Hãy chắc chắn bạn đã nắm vững kiến thức cơ bản trước khi bắt đầu.
YouTube video
Mục tiêu và Nguồn Dữ liệu
Mục tiêu của chúng ta là trích xuất dữ liệu từ trang Axie.zone. Hiểu rõ cơ chế tải dữ liệu của website là rất quan trọng. Bạn có thể tìm hiểu thêm về kiến thức cơ bản này trong bài viết trước của chuỗi bài viết này. Video hướng dẫn chi tiết cách trích xuất dữ liệu từ web bằng GraphQL và Google Apps Script cũng sẽ rất hữu ích.
Code Google Apps Script
Dưới đây là đoạn code được sử dụng trong video hướng dẫn:
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)
}
Giải thích Code
Đoạn code này thực hiện một yêu cầu POST đến API GraphQL của Axie Infinity để lấy dữ liệu. Các tham số được định nghĩa trong phần variables
cho phép bạn tùy chỉnh yêu cầu để lấy dữ liệu cụ thể. Hàm UrlFetchApp.fetch
được sử dụng để gửi yêu cầu và nhận phản hồi. Kết quả được ghi lại bằng Logger.log
.
Kết luận
Việc trích xuất dữ liệu từ website sử dụng GraphQL và Google Apps Script mở ra nhiều khả năng phân tích và xử lý dữ liệu mạnh mẽ. Hy vọng bài viết này đã cung cấp cho bạn những kiến thức cần thiết để bắt đầu. Hãy tìm hiểu thêm về các bài viết khác trong chuỗi để nắm vững kiến thức về GraphQL và ứng dụng của nó.
Discussion about this post