首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环遍历html列表并获取内容。

循环遍历html列表并获取内容。
EN

Stack Overflow用户
提问于 2021-03-09 13:49:42
回答 2查看 66关注 0票数 0

我正在处理一个具有以下结构的页面。

代码语言:javascript
复制
<section id='famous'>

<li class="list-entry" id="samething">
   <span class="list-item item-type">
      <a href="link-1">Movies</a>
   </span>

   <span class="list-item item-name item-title">
      <a href="link-2"> Title 1</a>
   </span>

   <span class="list-item item-uploaded"> 
      <a href="link-2"> Title 1</a>
   </span>
</li>


<li class="list-entry" id="samething">
   <span class="list-item item-type">
      <a href="link-1">Movies</a>
   </span>

   <span class="list-item item-name item-title">
      <a href="link-2"> Title 1</a>
   </span>

   <span class="list-item item-uploaded"> 
      <a href="link-2"> Title 1</a>
   </span>
</li>



<li class="list-entry" id="samething">
   <span class="list-item item-type">
      <a href="link-1">Movies</a>
   </span>

   <span class="list-item item-name item-title">
      <a href="link-2"> Title 1</a>
   </span>

   <span class="list-item item-uploaded"> 
      <a href="link-2"> Title 1</a>
   </span>
</li>

</section>

到目前为止,这是我的代码。

代码语言:javascript
复制
const puppeteer = require('puppeteer')

const main = async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()

  await page.goto('link-to-my-website')

  await page.waitForSelector("section[id='famous']");

  // const movies = await page.$$('#famous > li.list-entry');
  // for (let i = 0; i < 2; i++) {
  //   const element = movies[i];
  //   console.log(element);    
  // }

  const movies =  await page.$$('#famous > li.list-entry', el => el.innerText);
  movies.forEach(element => {
    console.log(element);
  });

  await browser.close()
}

main()

现在,我不知道如何将span的内容放入数组中。这样我就可以访问a标记中的链接。救命啊!!

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-09 14:15:51

根据木偶技师文献page.$$返回一个ElementHandle数组。这些元素还提供了一些选择方法评价方法。您应该能够查询所需的子元素。例如:

代码语言:javascript
复制
const movies =  await page.$$('#famous > li.list-entry');
for (const movie of movies) {
    const name = await movie.$eval('.item-name > a', el => el.innerText);
    const itemUploaded = await movie.$eval('.item-uploaded > a', el => el.innerText);
    console.log(name, itemUploaded);
}
票数 2
EN

Stack Overflow用户

发布于 2021-03-09 14:23:20

我想你可以用一个css选择器来完成这个任务?

代码语言:javascript
复制
const $$ = (selector, cb) => new Promise( resolve => 
  resolve([...document.querySelectorAll(selector)].map(cb) ) );

$$("#famous li.list-entry span a", el => el.innerText).then(r => console.log(r));

// or even simpler
$$("#famous a", el => el.innerText).then(r => console.log(r));
代码语言:javascript
复制
<section id='famous'>
  <ul>
    <li class="list-entry" id="samething">
      <span class="list-item item-type">
      <a href="link-1">Movies</a>
   </span>

      <span class="list-item item-name item-title">
      <a href="link-2"> Title 1</a>
   </span>

      <span class="list-item item-uploaded"> 
      <a href="link-2"> Title 2</a>
   </span>
    </li>
    <li class="list-entry" id="samething">
      <span class="list-item item-type">
      <a href="link-1">Movies</a>
   </span>

      <span class="list-item item-name item-title">
      <a href="link-2"> Title 1</a>
   </span>

      <span class="list-item item-uploaded"> 
      <a href="link-2"> Title 2</a>
   </span>
    </li>

    <li class="list-entry" id="samething">
      <span class="list-item item-type">
      <a href="link-1">Movies</a>
   </span>

      <span class="list-item item-name item-title">
      <a href="link-2"> Title 2</a>
   </span>

      <span class="list-item item-uploaded"> 
      <a href="link-2"> Title 1</a>
   </span>
    </li>
  </ul>
</section>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66548155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档