我希望在我的nodejs/cheerio 抓取脚本中提取韦恩县 EDA。
到目前为止,我可以获取电子邮件 (a) 和姓名 (strong),但无法追踪如何获取韦恩县 EDA,因为它不在标签中。有没有办法说“在第一个 br 标签后抓取文本”,或者是否有更好的方法来实现它?
供参考,这里有一些半工作代码
const rp = require('request-promise');
const $ = require('cheerio');
const url = 'https://www.wvedc.org/members/regular-members/';
rp(url)
.then(function (html) {
//success!
let length = $('div.content_page_content_text div strong', html).length;
let results = [];
let array = $('div.content_page_content_text div', html).get();
array.forEach((el) => {
results.push({
email: $(el).find('div a').text(),
name: $(el).find('div strong').text(),
//company: $(el).find('div br'), // need wayne county here
});
});
})
.catch(function (err) {
//handle error
});
编辑:dang,可能是重复的:使用 Cheerio 的 br 标记后的目标文本
这可能不是问题的完美答案,但这是您想要实现的目标的可行解决方案
const rp = require('request-promise');
const $ = require('cheerio');
const url = 'https://www.wvedc.org/members/regular-members/';
rp(url)
.then(function (html) {
//success!
let length = $('div.content_page_content_text div strong', html).length;
let results = [];
let array = $('div.content_page_content_text div', html).get();
array.forEach((el) => {
results.push({
email: $(el).find('div a').text(),
name: $(el).find('div strong').text(),
company: $(el).html().split("<br>")[1].substr(1)
});
});
})
.catch(function (err) {
//handle error
});