Skip to content

Instantly share code, notes, and snippets.

@wisetc
Created October 16, 2018 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wisetc/9cb45daeab0df0e280833452ed1a72b1 to your computer and use it in GitHub Desktop.
Save wisetc/9cb45daeab0df0e280833452ed1a72b1 to your computer and use it in GitHub Desktop.
快速获取爱上租城市的分区的商圈信息
const axios = require('axios');
const $ = require('cheerio');
async function visitHome() {
const res = await axios.get('https://nj.ishangzu.com/zufang/');
const hrefArr = Array.from(
$('.term-list', res.data)
.eq(1)
.find('a')
)
.map(item => ({ text: $(item).text(), value: $(item).attr('href') }))
.slice(1);
return hrefArr;
}
async function getWordList(href) {
const res = await axios.get(href);
const textArr = Array.from(
$('.term-list', res.data)
.eq(2)
.find('a')
)
.map(item => $(item).text())
.slice(1);
return textArr;
}
visitHome();
async function main() {
const hrefArr = await visitHome();
console.log(hrefArr);
const words = [];
for (let href of hrefArr) {
const textArr = await getWordList(href.value);
words.push({
[href.text]: textArr,
});
}
console.log(JSON.stringify(words));
return words;
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment