Skip to content

Instantly share code, notes, and snippets.

@1000copy
Last active April 29, 2022 06:42
Show Gist options
  • Save 1000copy/cb683384a9eff8f80c63f14b6cd1ad5f to your computer and use it in GitHub Desktop.
Save 1000copy/cb683384a9eff8f80c63f14b6cd1ad5f to your computer and use it in GitHub Desktop.
// v2ex不容易看到对话。于是我写了一个爬虫,抓出两人的对话,谁是谁非,大家自行判断。哈哈有趣。
/*
Title:从v2ex内提取两人的对话
Paramters:PostID,userid1,userid2
Procedure:
1. 提出对应帖子的全部回复
2. 过滤条件:是IDList内的id发出的帖子,并且帖子内@了另外一个id的
3. 帖子字段保留
API:
获取指定主题下的回复
GET topics/:topic_id/replies
可选参数:
p - 分页页码,默认为 1
完整例子:
GET https://www.v2ex.com/api/v2/topics/1/replies?p=2
Authorization: Bearer XXX
帖子ID 849548
topics/849548
*/
var fs = require('fs')
// 把你自己的secret写道文件secret.txt内
const secret = fs.readFileSync("secret.txt").toString();
console.log(secret)
async function fr (url){
var result = await fetch(url,{headers: {'Authorization': `Bearer ${secret}`}})
return await result.json()
}
(async(id,u1,u2)=>{
var r = await fr(`https://www.v2ex.com/api/v2/topics/${id}`)
var rc = r.result.replies
var page = Math.ceil(rc/20)
var arr = []
for (let index = 0; index < page; index++) {
var r = await fr(`https://www.v2ex.com/api/v2/topics/${id}/replies?p=`+(index+1))
r.result.forEach(element => {if(element.member.username==`${u1}` ||element.member.username==`${u2}`)arr.push([element.member.username,element.content])});
}
const result = arr.filter(word => word[1].includes(`@${u1}`) || word[1].includes(`@${u2}`));
result.forEach(element=>{
console.log()
console.log(`\n====${element[0]}====\n`,element[1])
})
// console.log(result);
})(849548,'Suddoo','glfpes')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment