Created
October 16, 2018 11:55
-
-
Save wisetc/9cb45daeab0df0e280833452ed1a72b1 to your computer and use it in GitHub Desktop.
快速获取爱上租城市的分区的商圈信息
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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