admin管理员组

文章数量:1400583

I have the below HTML structure

<div class ="container" id= "12">
    <div class="details" desc-type= "multiline">
        <a href="#">
            <div class="description"> Some Description </div>
        </a>
    </div>
</div>

And I scraping this using the below code

const SELECTOR =
    "div.container";

const movies = await page.$$eval(
    SELECTOR,
      nodes =>
        nodes.map(element => {
          return {
            movieID: element.getAttribute("id"),
          };
        } )    
    );

How can I modify the above code so that I can read desc-type= "multiline" and innerText of <div class="description">?

I have the below HTML structure

<div class ="container" id= "12">
    <div class="details" desc-type= "multiline">
        <a href="#">
            <div class="description"> Some Description </div>
        </a>
    </div>
</div>

And I scraping this using the below code

const SELECTOR =
    "div.container";

const movies = await page.$$eval(
    SELECTOR,
      nodes =>
        nodes.map(element => {
          return {
            movieID: element.getAttribute("id"),
          };
        } )    
    );

How can I modify the above code so that I can read desc-type= "multiline" and innerText of <div class="description">?

Share Improve this question edited Feb 27, 2019 at 0:50 hardkoded 21.8k3 gold badges61 silver badges74 bronze badges asked Nov 28, 2018 at 9:09 neo-technokerneo-technoker 3892 gold badges11 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

How about this?

const movies = await page.$$eval(
    SELECTOR,
      nodes =>
        nodes.map(element => {
          return {
            movieID: element.getAttribute("id"),
            descType: element.querySelector('[desc-type]').getAttribute('desc-type'), 
            description: element.querySelector(".description").innerText
          };
        } )    
    );

本文标签: javascriptAccessing child elements in puppeteerStack Overflow