文章目录
网站中动态渲染的页面需要请求ajax地址获取数据然后操作获取到的数据,存入文件
// 引入https模块
const https = require('https')
// 引入fs模块
const fs = require('fs')
const url = 'https://weibo.com/ajax/side/hotSearch'
// 伪造请求头,使用请求方法
const req = https.request( url , { method : 'get' , header:{
'authority': 'weibo.com',
'method': 'GET',
'path': '/ajax/side/hotSearch',
'scheme': 'https',
'accept': 'application/json, text/plain',
'accept-encoding': 'gzip, deflate',
'accept-language': 'zh-CN,zh;q=0.9',
'client-version': 'v2.39.11',
'cookie: SUB' :'_2AkMV8Va5f8NxqwJRmfsRy2zibYp3zg3EieKjradiJRMxHRl-yT9jqm4TtRB6PnF4VrUm_8R2sMVNbWPxXv32RSyaeK0g; UOR=www.fengfengzhidao.com,s.weibo.com,www.fengfengzhidao.com; SINAGLOBAL=8261570728013.232.1676544993660; ULV=1676544993765:1:1:1:8261570728013.232.1676544993660:; XSRF-TOKEN=qkag5L4a3VKkyOta0XWU4VbU; WBPSESS=mm07v0uQ8nV44TNSi6a9Lebp7dgHcnVYLsUiAsmisBaK6r-N6miZrPKTfFk95sDm5C-tGfyzcpx72fceiJuVVoH9bb6f5m14tS_CjUDpn3Ql_R3fAgQx0jcjKGNCMymJe0upWL2bHmxhlSK5QbBFvGKlwip_tdk8fAU3AUvnDdk=',
'referer': 'https://weibo.com/newlogin?tabtype=search&gid=&openLoginLayer=0&url=',
}} , res=>{
let chunks = []
// 监听网页数据
res.on('data', chunk=>{
chunks.push(chunk)
})
// 监听网页数据完成
res.on('end', ()=>{
// 接收爬取的数据进行字符串转化
const result = Buffer.concat(chunks).toString('utf-8')
// 字符串转换为对象
let hotArr = JSON.parse(result).data.realtime
// 定义一个新数组,用于存放筛选后的数据
let newHotArr = []
// 遍历数据,指定自己需要的数据
hotArr.forEach(item => {
newHotArr.push({
'note':item.note,
'rank':item.rank
})
})
// 使用fs模块,将数据写入文件中
fs.writeFile('./wb_news/wb_news.text' , JSON.stringify(newHotArr) , function(err){
if(err){
console.log('文件写入失败:' + err.message);
}else{
console.log('文件写入成功');
}
})
})
})
// 发送请求
req.end()