You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
superagent.get(reptileUrl).set('Connection','keep-alive').set('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36').set('Host','www.xiachufang.com').end(function(err,res){if(err){throwError(err);}// 处理数据});
3.解析数据
superagent.get(reptileUrl).set('Connection','keep-alive').set('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36').set('Host','www.xiachufang.com').end(function(err,res){if(err){throwError(err);}// 处理数据/** * res.text 包含未解析前的响应内容 * 通过cheerio的load方法解析整个文档,就是html页面所有内容. */let$=cheerio.load(res.text);// console.log($.html());});
需要使用的模块
superagent 页面数据下载
超级代理,是一个http请求模块,常用于get、post等请求,可以实现请求操作,还有一些表单post操作,模拟ajax请求等,支持链式调用。
扩展
:superagent-charsetsuperagent只支持UTF-8,用了这个库可以指定编码,当用superagent爬出乱码时可以用它。
cheerio 页面数据解析
可以使用大量jQuery的语法来获取你爬虫请求到的数据
demo 下厨房里面的菜谱数据
2.发请求获取数据
现在可以运行
node app.js
看一下请求。此时会报错。获得了一个Forbidden错误。网站对于访问进行了检查,我们复制请求头,重写网络请求。3.解析数据
4.分析页面结构,拼接数据
现在需要打开下厨房网站本周最受欢迎页面,列表展示的信息都是服务端渲染的。爬虫就是获取服务端渲染的数据,ajax获取的数据的话,直接调api接口即可。我们就以获取第一列数据为例子。
定义数据结构
5.保存数据到本地文件
完整代码
The text was updated successfully, but these errors were encountered: