javascript - 如何在cheerio中没有元素/类的br标签之间嵌套文本?
问题描述
我希望在我的 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
});
推荐阅读
- c# - EF 5 混合进程时,linq 不在事务范围内工作
- debian - 容器 - 如何在没有密码的情况下从 www-data 切换到 root
- sql - 过滤和删除表中的重复数据 - SQL Query
- python - Pandas pivot_table 将行重新排列为列
- github - 尝试使用 api 从 git 访问文件时出现 404
- import - #TigerGraph 导入:每种关系类型一个 csv?
- amazon-web-services - UnauthorizedAccessException - 拒绝访问路径 <>。AWS 托管的 .NET MVC 应用程序
- iphone - 在 iPhone 的 WhatsApp 链接预览中强制使用小图像
- python - Django Rest Framework 中的常用模型
- c# - 构建 HasForeignKey 时抛出 System.NullReferenceException