网站首页 > 开源技术 正文
今天爬取某个古老的网站的数据,发现中文乱码。
这个网站从外观上看也够古老的,是偏政企类网页,这种项目一般也都是外包出去的,技术更新速度也是可想而知,里面竟然还是写死的gb2312,还有三层table嵌套。
在网上搜了下,有不少文章说的可能方法是过时,反正我实测没效果。
在npm上搜了下superagent,发现官方就提到了一个charset插件superagent-charset,superagent-charset的官方github上就给出来示例代码。
const should = require('should')
const request = require('superagent')
require('superagent-charset')(request) // install charset
describe('Basic Test', function() {
it('it works', function(done) {
request.get('http://www.sohu.com/')
.charset('gbk')
.end((err, res) => {
res.text.should.match(/搜狐/)
done(err)
})
})
})
果然,搞定了。
const superagent= require('superagent');
require('superagent-charset')(superagent)
const cheerio = require('cheerio');
function getResponse(url, charset = 'utf8') {
return new Promise((resolve, reject) => {
superagent.get(url)
.buffer(true).charset(charset)
.end((err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
}
猜你喜欢
- 2024-10-21 霸榜掘金!轻量级请求策略库 alova 出炉!
- 2024-10-21 对于现代 Web 应用除了美观要求之外,对产品体验度要求高
- 2024-10-21 5 个顶级的 JavaScript Ajax 组件和库
- 2024-10-21 用 async 模块控制并发数(async await并发)
- 2024-10-21 package-lock.json的作用?(package lock.json)
- 2024-10-21 为什么 JS 开发者更喜欢 Axios 而不是 Fetch?
- 2024-10-21 Node.js爬虫实战 - 爬你喜欢的(node爬取数据)
- 2024-10-21 node.js爬虫-校园网模拟登录(校园网模拟登陆)
- 2024-10-21 IMT星际云每周资讯-20190111(星际云官网)
- 2024-10-21 nodejs,express,koa爬虫实战(node网络爬虫)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)