这里分享如何使用 nodejs 爬取快手用户发表的视频,仅供学习使用。
const { GraphQLClient, gql } = require("graphql-request");
//关键是 header 中要包含合格的 Cookie 值
async function app() {
const url = "https://www.kuaishou.com/graphql";
const client = new GraphQLClient(url);
const query = gql`
{
visionProfilePhotoList(
pcursor: ""
userId: "3x4yuv5jbnnvyss"
page: "profile"
) {
result
llsid
webPageArea
feeds {
type
author {
id
name
following
headerUrl
headerUrls {
cdn
url
__typename
}
__typename
}
tags {
type
name
__typename
}
photo {
id
duration
caption
likeCount
realLikeCount
coverUrl
coverUrls {
cdn
url
__typename
}
photoUrls {
cdn
url
__typename
}
photoUrl
liked
timestamp
expTag
animatedCoverUrl
stereoType
videoRatio
profileUserTopPhoto
__typename
}
canAddComment
currentPcursor
llsid
status
__typename
}
hostName
pcursor
__typename
}
}
`;
const variables = {
pcursor: "",
userId: "3x4yuv5jbnnvyss",
page: "profile",
};
//Cookie最重要,其他好像都不需要
const requestHeaders = {
Cookie:
"clientid=3; did=web_233037eca7abf4c900e88b73742835ee; client_key=65890b29; kpf=PC_WEB; kpn=KUAISHOU_VISION",
Referer: "https://www.kuaishou.com/profile/3x4yuv5jbnnvyss",
Origin: "https://www.kuaishou.com",
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55",
};
const data = await client
.request(query, variables, requestHeaders)
.catch((err) => {
console.log("出错:", err);
});
console.log(data);
}
app();