gpt4 book ai didi

javascript - 网络抓取和 promise

转载 作者:太空宇宙 更新时间:2023-11-04 03:02:47 24 4
gpt4 key购买 nike

我正在使用cheerio和node来进行网络抓取,但是我在promise方面遇到了问题。我可以从页面中抓取文章列表,但在该列表中,我们有更多单个页面的链接。我还需要为列表中的每个项目抓取单页。我将向您展示我的代码以获得更好的解决方案。

import rp from 'request-promise'
import cheerio from 'cheerio'
import conn from './connection'

const flexJob = `https://www.flexjobs.com`
const flexJobCategory = ['account-management', 'bilingual']

class WebScraping {

//list of article e.g for page 2
results = [] // [[title], [link for page],...]
contentPage = [] //content for each page

scrapeWeb(link) {
let fullLink = `${link}/jobs/${flexJobCategory[1]}?page=2`
const options = {
uri: fullLink,
transform(body) {
return cheerio.load(body)
}
}
rp(options)
.then(($) => {
console.log(fullLink)
$('.featured-job').each((index, value) => {

//html nodes
let shortDescription = value.children[1].children[1].children[3].children[1].children[1].children[0].data
let link = value.children[1].children[1].children[1].children[1].children[1].children[0].attribs.href
let pageLink = flexJob + '' + link
let title = value.children[1].children[1].children[1].children[1].children[1].children[0].children[0].data
let place = value.children[1].children[1].children[1].children[1].children[3].children[1].data
let jobType = value.children[1].children[1].children[1].children[1].children[3].children[0].children[0].data
this.results.push([title, '', pageLink.replace(/\s/g, ''), '', shortDescription.replace(/\n/g, ''), place, jobType, 'PageContent::: '])
})
})
.then(() => {
this.results.forEach(element => {
console.log('link: ', element[2])
this.scrapePage(element[2])
});
})
.then(() => {
console.log('print content page', this.contentPage)
})
.then(() => {
//this.insertIntoDB()
console.log('insert into db')
})
.catch((err) => {
console.log(err)
})

}

/**
* It's going to scrape all pages from list of jobs
* @param {Any} pageLink
* @param {Number} count
*/
scrapePage(pageLink) {
let $this = this
//console.log('We are in ScrapePage' + pageLink + ': number' + count)
//this.results[count].push('Hello' + count)
let content = ''
const options = {
uri: pageLink,
transform(body) {
return cheerio.load(body)
}
}
rp(options)
.then(($) => {
//this.contentPage.push('Hello' + ' : ');
console.log('Heloo')
})
.catch((err) => {
console.log(err)
})
}
/**
* This method is going to insert data into Database
*/
insertIntoDB() {
conn.connect((err) => {
var sql = "INSERT INTO contact (title, department, link, salary, short_description, location, job_type, page_detail) VALUES ?"
var values = this.results
conn.query(sql, [values], function (err) {
if (err) throw err
conn.end()
})
})
}
}
let webScraping = new WebScraping()
let scrapeList = webScraping.scrapeWeb(flexJob)

因此,在“scrapeWeb”方法中,在第二个“.then”处,我调用“scrapePage”方法,但是,在“scrapePage”方法内的 Promise 之前执行了第三个 Promise。

最佳答案

在那个阶段你需要更多的控制流程。在所有调用都得到解决之前,您不希望 .then() 的 promise 得到解决。

您可以使用 Promise 库,例如 bluebird为您想要运行的所有结果执行 Promise.eachPromise.map

或者使用 async/await 进行设置,例如 .then(async () => {}) 并且不要使用 .forEach

for(let element of this.results){
console.log('link: ', element[2])
await this.scrapePage(element[2])
}

关于javascript - 网络抓取和 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50177779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com