试图在JavaScript中获取动态字符串的最后一个字符?
我正在用JS创建一个新闻刮板应用程序,它可以拉入文章及其描述 下面是刮刀;然而,当拉入描述时,它也从链接中拉入文本形式的“ReadMe”。我仍然想把摘录拉进去,但去掉最后的“阅读更多”:试图在JavaScript中获取动态字符串的最后一个字符?,javascript,jquery,node.js,mongoose,cheerio,Javascript,Jquery,Node.js,Mongoose,Cheerio,我正在用JS创建一个新闻刮板应用程序,它可以拉入文章及其描述 下面是刮刀;然而,当拉入描述时,它也从链接中拉入文本形式的“ReadMe”。我仍然想把摘录拉进去,但去掉最后的“阅读更多”: app.get("/scraper", function(req, res) { // Grabs the body of the html with request request("https://techcrunch.com/", function(error, response, html) {
app.get("/scraper", function(req, res) {
// Grabs the body of the html with request
request("https://techcrunch.com/", function(error, response, html) {
// Load into cheerio with $ as a shorthand selector
var $ = cheerio.load(html);
// Grabs the title, description, and link within the block-content class.
$(".block-content").each(function(i, element) {
// Save an empty result object
var result = {};
// Saves them as properties of the result object
result.title = $(this).find(".post-title").children("a").text();
result.link = $(this).find("a").children(".excerpt").attr("href");
result.description =$(this).find(".excerpt").text();
console.log(result);
if (result.title && result.link && result.description) {
// Creates a new entry using the article model
var entry = new Article(result);
// Saves that entry to the db
entry.save(function(err, doc, next) {
// Log any errors
if (err) {
console.log(err);
}
});
}
});
这就是目标;您可以在说明中看到“阅读更多”
{ title: 'Snapcart raises $10M to shed light on consumer spending in emerging markets',
link: 'https://techcrunch.com/2017/10/25/snapcart-raises-10m/',
description: 'Taking on a giant like the $15 billion research firm Nielsen is no easy task. But tucked away in Southeast Asia, Snapcart is a two-year old company that is making progress by shining light on the black box that is consumer spending in emerging markets. Read More' }
使用将从字符串中删除
阅读更多内容。请提问。
result.description =$(this).find(".excerpt").text().replace(' Read More', '');