- 我在这里所做的是通过Web截取instagram的标签URL。
- 我在终端VSCode上收到的是数组形式的数据,很棒。
- 我想做的是向浏览器路由(
app.get/data
)发送响应,该响应将显示我在终端上看到的所有这些数据。
const rp = require('request-promise');
const cheerio = require('cheerio');
const express = require('express')
const app = express()
const port = 3000
app.get('/data', (req, res, next) => {
//res.??????
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
let keyWord = "hair"
let URL = `https://www.instagram.com/explore/tags/${keyWord}/`
rp(URL)
.then((html) => {
let hashtags = scrapeHashtags(html);
hashtags = removeDuplicates(hashtags);
hashtags = hashtags.map(ele => "#" + ele)
console.log(hashtags);
})
.catch((err) => {
console.log(err);
});
const scrapeHashtags = (html) => {
var regex = /(?:^|\s)(?:#)([a-zA-Z\d]+)/gm;
var matches = [];
var match;
while ((match = regex.exec(html))) {
matches.push(match[1]);
}
return matches;
}
const removeDuplicates = (arr) => {
let newArr = [];
arr.map(ele => {
if (newArr.indexOf(ele) == -1) {
newArr.push(ele)
}
})
return newArr;
}