How to generate an object with the elements found in a given jQuery selection

问题: I have the following jQuery selection, helped by LGSon's answer to an earlier question... $('div.content__article-body > *').not('aside').each( function( index, value...


I have the following jQuery selection, helped by LGSon's answer to an earlier question...

$('div.content__article-body > *').not('aside').each( function( index, value ) {
  console.log( value.outerHTML );

However, I need to learn how to write this in a way consistent with the result portion of my code below...

I guess I need to chain the outerHTML element on to the jQuery line, but I have not had much success with this.

In this example, the selection is for the "entry".

function pageFunction(context) {

    // Called on every page the crawler visits, use it to extract data from it
    var $ = context.jQuery;

    // If page is START or a LIST,
    if (context.request.label === 'START' || context.request.label === 'LIST') {


        // First, gather LIST page
        $('ol.pagination li a').each(function() {
                url: window.location.origin + $(this).attr('href'),
                label: 'LIST'

        // Then, gather every DETAIL page
                url: window.location.origin + $(this).attr('href'),
                label: 'DETAIL'

    // If page is actually a DETAIL target page
    } else if (context.request.label === 'DETAIL') {

        /* context.skipLinks(); */

        var tags = [];
        $('span.tags a').each( function() {

        result = {
            "title": $('h1.entry-title').text(),
            "excerpt": $('div.content-blog__body p strong:first').text().trim(),
            "entry": $('div.content-blog__body').html().trim(),
            "datestamp": $('meta[property="article:published_time"]').attr('content'),
            tags: tags

    return result;


Here is a simplified pageFunction, to show how to search a result set, using .find().

As I now know how you meant (in linked question), this answer from the same (which I think should be credited as well, plus 1 from me) would get you a result set to work with, instead of an array of elements, which mine gives, though here I used jQuery's append() when create the new object.

Stack snippet

var $res = $('<div></div>').append( $('div.content__article-body') );

console.log( pageFunction() );

function pageFunction() {

        result = {
            "title": $res.find('h2').text(),
            "excerpt": $res.find('h2 ~ p:first').text().trim()

    return result;
<script src=""></script>

<div class="content__article-body from-content-api js-article__body" itemprop="articleBody" data-test-id="article-review-body">
<p>A wise man once <a href="" rel="nofollow" data-link-name="in body link" class="u-underline">said</a>: “If you’re not paying for it, you’re the product.” That’s what is happening in the magazine world, as <a href="" data-link-name="in body link" class="u-underline">NME abandons its cover price</a> after 63 years, to go free.<br></p>
<p>In doing so, the title is turning its readers from customers in to advertiser targets. But NME is far from the first property to try this trick. <a href="" rel="nofollow" data-link-name="in body link" class="u-underline">The Village Voice</a>, the <a href="" rel="nofollow" data-link-name="in body link" class="u-underline">Shrewsbury Chronicle</a>, the <a href=";sjid=KSgMAAAAIBAJ&amp;pg=1120%2C535142" rel="nofollow" data-link-name="in body link" class="u-underline">Standard</a> of Hong Kong, <a href="" rel="nofollow" data-link-name="in body link" class="u-underline">Friends Reunited</a>, the <a href="" rel="nofollow" data-link-name="in body link" class="u-underline">Evening Standard</a>, the <a href=";spage=common/contrinity001.htm" rel="nofollow" data-link-name="in body link" class="u-underline">Fulham and Hammersmith Chronicle</a>, Time Out in <a href="" rel="nofollow" data-link-name="in body link" class="u-underline">London</a> and <a href="" rel="nofollow" data-link-name="in body link" class="u-underline">New York</a>, and now <a href="" data-link-name="in body link" class="u-underline">parts of the Sun’s website</a> are all among those to have dropped their fees in favour of frees.</p>
<p>But what has become of the converts? And what do their fortunes since tell us, <a href="" data-link-name="auto-linked-tag" data-component="auto-linked-tag" class="u-underline">NME</a> and other operators about the key ingredients for a successful switch?</p>
<aside class="element element-rich-link element--thumbnail element-rich-link--not-upgraded" data-component="rich-link" data-link-name="rich-link-1 | 1">
<div class="rich-link">
<div class="rich-link__container">
<div class="rich-link__header">
<h1 class="rich-link__title">Old media's prospects have improved but the new media threat looms large</h1>
<div class="rich-link__read-more">
<div class="rich-link__arrow">
<span class="inline-arrow-in-circle inline-icon ">
<svg width="24" height="24" viewbox="0 0 24 24" class="inline-arrow-in-circle__svg inline-icon__svg">
<path class="rich-link__arrow-icon" d="M12 0C5.373 0 0 5.373 0 12s5.373 12 12 12 12-5.373 12-12S18.627 0 12 0m.21 19l-.637-.668 4.888-6.326H4.996v-1.01h11.465l-4.888-6.333.637-.668L19 11.153v.685l-6.79 7.157" fill-rule="evenodd"/>
</svg> </span>
<div class="rich-link__read-more-text">
Read more
<a class="rich-link__link u-faux-block-link__overlay" href="" aria-label="Old media's prospects have improved but the new media threat looms large"></a>
<h2>1. Know your predicament</h2>
<p>It would be nice to say the switch to free comes either out of benevolence or out of a market opportunity. The reality is, operators invariably flick the switch under external pressure of a poor business outlook – no amount of dressing up as a “bold relaunch” can change that.</p>
<p>NME’s circulation was falling by <a href="" data-link-name="in body link" class="u-underline">around a fifth annually</a>. The Evening Standard was frustrated to find itself far behind national dailies on ad planners’ schedules, locking it out of big brand bucks, according to someone familiar with the strategy to switch.</p>
<p>The Village Voice dropped its cover charge in 1996 after being eaten into by rival New York Press being given away in green sidewalk boxes, while Hong Kong’s Standard newspaper was forced to act when the government removed a decree that listed companies must buy classified ads in newspapers. But the key – as anyone who has heard the “<a href="" rel="nofollow" data-link-name="in body link" class="u-underline">boiling frog</a>” allegory will know – is to recognise when it is your time to change tack.</p>

  • 发表于 2018-07-05 20:18
  • 阅读 ( 254 )
  • 分类:sof


请先 登录 后评论


作家榜 »

  1. 小编 文章