Files
ClassContent/历届学生/front-end-team/项目练习/上课项目/爬虫_快手/index.js
2024-09-27 02:06:13 +08:00

112 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// noinspection JSIgnoredPromiseFromCall
const express = require('express')
const { GraphQLClient, gql } = require("graphql-request");
const request = require('request');
const fs = require('fs');
const path = require('path');
const app = express()
const port = 3000
// 快手请求的基本地址
const url = "https://www.kuaishou.com/graphql";
// 页数
var pcursor = "";
// 用户的唯一id,用来作为文件名称 & 接口参数
var userId = "";
// 存放每一页爬的数据
var feeds = [];
// feeds的下标
var index = 0;
const client = new GraphQLClient(url)
app.get('/down', async (req, res) => {
userId = req.query.id;
getFeeds()
res.end('Hello World!')
});
async function getFeeds() {
const query = gql`
query visionProfilePhotoList($pcursor: String, $userId: String, $page: String, $webPageArea: String) {
visionProfilePhotoList(pcursor: $pcursor, userId: $userId, page: $page,webPageArea: $webPageArea) {
result,llsid,webPageArea,feeds {
type,author {
id,name,following,headerUrl,headerUrls {
cdn,url
}
},
tags {
type,name
},
photo {
id,duration,caption,likeCount,realLikeCount,coverUrl,coverUrls {
cdn,url
},
photoUrls {
cdn,url
},
photoUrl,liked,timestamp,expTag,animatedCoverUrl,stereoType,videoRatio,profileUserTopPhoto
},
canAddComment,currentPcursor,llsid,status
},
hostName,pcursor
}
}
`
const variables = {
pcursor: pcursor,
userId: userId,
page: 'profile'
};
// 通过快手后端的接口验证
const requestHeaders = {
Cookie: 'kpf=PC_WEB; kpn=KUAISHOU_VISION; clientid=3; did=web_cf504812c13b0458cbdcc5bc9ea39691; client_key=65890b29; userId=58344290; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABq_TLWokR1HCFOInz6xuXy_kvuyQzzFhSSHR6_VuGW5G8r7X0I7jaXdIsvNLtzMihRfZy3IC6ZuErdyrv9JpRcK-I1orA8PsLE5yjof_RUiU85_sdhL_6PHbQY-gBM9-5SGcguwtpmc6gGpP6ztlEeucgq4S3KC668-TGkcoNATOo_JmyYuf1jvjxaHtwe1dznn-qQYr1ypIHiwzl00MKRBoSnIqSq99L0mk4jolsseGdcwiNIiC82HgSYpfCs7fFoo9D2LxFDjCTkWKnva615o7eqi8GgSgFMAE; kuaishou.server.web_ph=ccabd46f69a02c96deaa4092dc0d1595c15e',
Host: "www.kuaishou.com",
Origin: "https://www.kuaishou.com",
Referer: "https://www.kuaishou.com/profile/3x3f8rfq39drzey",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36"
}
const { visionProfilePhotoList } = await client.request(query, variables, requestHeaders)
pcursor = visionProfilePhotoList.pcursor;
feeds = visionProfilePhotoList.feeds;
downVideo()
// feeds.forEach((v,i) => {
// console.log(v.photo.photoUrl)
// downVideo(v.photo.photoUrl,v.photo.id,)
// // if (i == feeds.length - 1) {
// // console.log("已经下载到最后一条视频")
// // }
// })
}
function downVideo() {
if (!fs.existsSync(`mp4/${userId}`)) {
fs.mkdirSync(path.join(__dirname, `/mp4/${userId}`), 0o777)
}
request(feeds[index].photo.photoUrl).pipe(fs.createWriteStream(path.join(__dirname, `/mp4/${userId}/${feeds[index].photo.id}.mp4`)))
index += 1;
if (index < feeds.length) {
downVideo()
} else {
index = 0;
feeds = [];
if (pcursor != "no_more") {
getFeeds();
}
}
}
app.listen(port, () => {
console.log(`you app is run http://127.0.0.1:${port}`);
});