试图获取 JavaScript 中动态字符串的最后一个字符?
Trying to get the last characters of dynamic string in JavaScript?
我正在用 JS 创建一个新闻抓取应用程序,用于提取文章及其描述。
下面是刮板;但是,在提取描述时,它还会从 link 中提取 "read me" 作为文本。我仍想摘录,但删除最后的阅读更多内容:
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);
}
});
}
});
这是对象;你可以在描述中看到 "read more"。
{ title: 'Snapcart raises M 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 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', '');
使用 replace 将从字符串中删除 Read More
。
我正在用 JS 创建一个新闻抓取应用程序,用于提取文章及其描述。
下面是刮板;但是,在提取描述时,它还会从 link 中提取 "read me" 作为文本。我仍想摘录,但删除最后的阅读更多内容:
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);
}
});
}
});
这是对象;你可以在描述中看到 "read more"。
{ title: 'Snapcart raises M 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 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', '');
使用 replace 将从字符串中删除 Read More
。