@
dibage #2 经过半个小时的调试与测试,终于把煎蛋的代码调好啦!
这里附上:
解析数据
请求 GET
地址
http://jandan.net/返回 html
请求头,添加一个`User-Agent`(我是在 pc 站点界面调试的,所获的内容较丰富):
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
最后就是解析代码啦!
``` js
var result = [];
var temp = $HTML.split('<div class="post f list-post">');
temp.shift();
temp.map(function (tmp) {
var title = tmp.split('<h2>')[1].split('>')[1].split('<')[0];
var link = tmp.split('<h2>')[1].split('href="')[1].split('"')[0];
var description = tmp.split('<h2>')[1].split('</div>')[1].split('<')[0].trim();
var avatar;
try{
avatar = tmp.split('src="')[1].split('"')[0];
} catch(e) {
avatar = tmp.split('original="')[1].split('"')[0];
}
// 分类
var category = tmp.split('tag">')[1].split('<')[0];
// 点赞数
var zan = tmp.split('class="zan-icon"')[1].split('<')[0].match(/\d+/)[0];
// 评论数
var comment = tmp.split('comment-link')[1].split('>')[1].split('<')[0];
result.push({
title: title, link: link, description: description, avatar: avatar.startsWith('//') ? 'http:'+avatar:avatar,
category: [category],
tags: [{
icon: 'ios-chatbubbles',
text: comment
}, {
icon: 'ios-thumbs-up',
text: zan
}]
});
});
return result;
```
测试方式:桌面浏览器打开煎蛋网,打开控制台,在输入上边代码执行之前,先模拟以下获取$HTML 数据:
``` js
fetch('
http://jiandan.net/').then(ret => rete.text()).then(html => {
$HTML = html;
})
```
获取到$HTML 后,执行上述解析代码,完美~~~
附上预览图片:
https://i.loli.net/2018/02/03/5a75c75062a58.png祝开发愉快!