网络爬虫开发(三)-爬虫基础——爬取前端渲染网站的注意事项 & 分析ajax请求并爬取(伪造header)-模拟真实浏览器的请求头
爬取新闻信息
爬取目标:http://www.itcast.cn/newsvideo/newslist.html
大部分新闻网站,现在都采取前后端分离的方式,也就是前端页面先写好模板,等网页加载完毕后,发送Ajax再获取数据,将其渲染到模板中。所以如果使用相同方式来获取目标网站的HTML页面,请求到的只是模板,并不会有数据:
此时,如果还希望使用当前方法爬取数据,就需要分析该网站的ajax请求是如何发送的,可以打开network面板来调试:
分析得出对应的ajax请求后,找到其URL,向其发送请求即可
代码如下:
// 引入http模块 const http = require('http') // 创建请求对象 (此时未发送http请求) const url = 'http://www.itcast.cn/news/json/f1f5ccee-1158-49a6-b7c4-f0bf40d5161a.json' let req = http.request(url, res => {
// 异步的响应 // console.log(res) let chunks = [] // 监听data事件,获取传递过来的数据片段 // 拼接数据片段 res.on('data', c => chunks.push(c)) // 监听end事件,获取数据完毕时触发 res.on('end', () => {
// 拼接所有的chunk,并转换成字符串 ==> html字符串 // console.log(Buffer.concat(chunks).toString('utf-8')) let result = Buffer.concat(chunks).toString('utf-8') console.log(JSON.parse(result)) }) }) // 将请求发出去 req.end()
如果遇到请求限制,还可以模拟真实浏览器的请求头:
// 引入http模块 const http = require('http') const cheerio = require('cheerio') const download = require('download') // 创建请求对象 (此时未发送http请求) const url = 'http://www.itcast.cn/news/json/f1f5ccee-1158-49a6-b7c4-f0bf40d5161a.json' let req = http.request(url, {
headers: {
"Host": "www.itcast.cn", "Connection": "keep-alive", "Content-Length": "0", "Accept": "*/*", "Origin": "http://www.itcast.cn", "X-Requested-With": "XMLHttpRequest", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36", "DNT": "1", "Referer": "http://www.itcast.cn/newsvideo/newslist.html", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", "Cookie": "UM_distinctid=16b8a0c1ea534c-0c311b256ffee7-e--16b8a0c1ea689c; bad_idb2f10070-624e-11e8-917f-9fb8db4dc43c=8e1dcca1-9692-11e9-97fb-e5908bcaecf8; parent_qimo_sid_b2f10070-624e-11e8-917f-9fb8db4dc43c=921b3900-9692-11e9-9a47-855e632e21e7; CNZZDATA=--null%7C; cid_litiancheng_itcast.cn=TUd3emFUWjBNV2syWVRCdU5XTTRhREZs; PHPSESSID=j3ppafq1dgh2jfg6roc8eeljg2; CNZZDATA=cnzz_eid%3D--http%253A%252F%252Fmail.itcast.cn%252F%26ntime%3D; Hm_lvt_0cb375a2eb74efffa6c71ee607=,; qimo_seosource_22bdcd10-6250-11e8-917f-9fb8db4dc43c=%E7%AB%99%E5%86%85; qimo_seokeywords_22bdcd10-6250-11e8-917f-9fb8db4dc43c=; href=http%3A%2F%2Fwww.itcast.cn%2F; bad_id22bdcd10-6250-11e8-917f-9fb8db4dc43c=f2f41b71-a7a4-11e9-93cc-9ba8cb; nice_id22bdcd10-6250-11e8-917f-9fb8db4dc43c=f2f41b72-a7a4-11e9-93cc-9ba8cb; openChat22bdcd10-6250-11e8-917f-9fb8db4dc43c=true; parent_qimo_sid_22bdcd10-6250-11e8-917f-9fb8db4dc43c=fc61e520-a7a4-11e9-94a8-01dabdc2ed41; qimo_seosource_b2f10070-624e-11e8-917f-9fb8db4dc43c=%E7%AB%99%E5%86%85; qimo_seokeywords_b2f10070-624e-11e8-917f-9fb8db4dc43c=; accessId=b2f10070-624e-11e8-917f-9fb8db4dc43c; pageViewNum=2; nice_idb2f10070-624e-11e8-917f-9fb8db4dc43c=20d2a1d1-a7a8-11e9-bc20-e71d1b8e4bb6; openChatb2f10070-624e-11e8-917f-9fb8db4dc43c=true; Hm_lpvt_0cb375a2eb74efffa6c71ee607=" } }, res => {
// 异步的响应 // console.log(res) let chunks = [] // 监听data事件,获取传递过来的数据片段 // 拼接数据片段 res.on('data', c => chunks.push(c)) // 监听end事件,获取数据完毕时触发 res.on('end', () => {
// 拼接所有的chunk,并转换成字符串 ==> html字符串 // console.log(Buffer.concat(chunks).toString('utf-8')) let result = Buffer.concat(chunks).toString('utf-8') console.log(JSON.parse(result)) }) }) // 将请求发出去 req.end()
注意:请求头的内容,可以先通过真正的浏览器访问一次后获取
实例
test/news_list.js
// 引入http模块 const http = require('http') // let url = 'http://www.itcast.cn/newsvideo/newslist.html' let url = 'http://www.itcast.cn/news/json/f1f5ccee-1158-49a6-b7c4-f0bf40d5161a.json' // 创建请求对象 let req = http.request( url, {
method: 'post', headers: {
Host: 'www.itcast.cn', Connection: 'keep-alive', 'Content-Length': '0', Accept: '*/*', Origin: 'http://www.itcast.cn', 'X-Requested-With': 'XMLHttpRequest', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36', DNT: '1', Referer: 'http://www.itcast.cn/newsvideo/newslist.html', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', Cookie: 'UM_distinctid=16b8a0c1ea534c-0c311b256ffee7-e--16b8a0c1ea689c; bad_idb2f10070-624e-11e8-917f-9fb8db4dc43c=8e1dcca1-9692-11e9-97fb-e5908bcaecf8; parent_qimo_sid_b2f10070-624e-11e8-917f-9fb8db4dc43c=921b3900-9692-11e9-9a47-855e632e21e7; CNZZDATA=--null%7C; cid_litiancheng_itcast.cn=TUd3emFUWjBNV2syWVRCdU5XTTRhREZs; PHPSESSID=j3ppafq1dgh2jfg6roc8eeljg2; CNZZDATA=cnzz_eid%3D--http%253A%252F%252Fmail.itcast.cn%252F%26ntime%3D; Hm_lvt_0cb375a2eb74efffa6c71ee607=,; qimo_seosource_22bdcd10-6250-11e8-917f-9fb8db4dc43c=%E7%AB%99%E5%86%85; qimo_seokeywords_22bdcd10-6250-11e8-917f-9fb8db4dc43c=; href=http%3A%2F%2Fwww.itcast.cn%2F; bad_id22bdcd10-6250-11e8-917f-9fb8db4dc43c=f2f41b71-a7a4-11e9-93cc-9ba8cb; nice_id22bdcd10-6250-11e8-917f-9fb8db4dc43c=f2f41b72-a7a4-11e9-93cc-9ba8cb; openChat22bdcd10-6250-11e8-917f-9fb8db4dc43c=true; parent_qimo_sid_22bdcd10-6250-11e8-917f-9fb8db4dc43c=fc61e520-a7a4-11e9-94a8-01dabdc2ed41; qimo_seosource_b2f10070-624e-11e8-917f-9fb8db4dc43c=%E7%AB%99%E5%86%85; qimo_seokeywords_b2f10070-624e-11e8-917f-9fb8db4dc43c=; accessId=b2f10070-624e-11e8-917f-9fb8db4dc43c; pageViewNum=2; nice_idb2f10070-624e-11e8-917f-9fb8db4dc43c=20d2a1d1-a7a8-11e9-bc20-e71d1b8e4bb6; openChatb2f10070-624e-11e8-917f-9fb8db4dc43c=true; Hm_lpvt_0cb375a2eb74efffa6c71ee607=', }, }, (res) => {
let chunks = [] res.on('data', (chunk) => chunks.push(chunk)) res.on('end', () => {
let result = Buffer.concat(chunks).toString('utf-8') console.log(JSON.parse(result).data) }) } ) // 发送请求 req.end()
到此这篇网络爬虫开发(三)-爬虫基础——爬取前端渲染网站的注意事项 & 分析ajax请求并爬取(伪造header)-模拟真实浏览器的请求头的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/qdkf/10724.html