简体   繁体   English

使用 Cheerio 获取元素内部的元素

[英]Getting elements inside of elements using Cheerio

Let's say I have some HTML like this:假设我有一些这样的 HTML:

<body>
 <a href="link A">Hello</a>
 <div class="main">
  <a href="link A">Hello</a>
 </div>
<body>

I want to use Cheerio to get the links that are only contained inside the div with the class main.我想使用 Cheerio 获取仅包含在类 main 的 div 中的链接。 How would I even start to go about doing this?我将如何开始做这件事?

So far all I can even guess is doing something like:到目前为止,我什至能猜到的是做类似的事情:

$ = cheerio.load(body);
content = $('div.main');

But this obviously doesn't work because it never even tries to access any 'a' element inside of content (this is mostly because I have no clue how).但这显然不起作用,因为它甚至从未尝试访问内容中的任何“a”元素(这主要是因为我不知道如何访问)。

All help is appreciated, thanks in advance.感谢所有帮助,提前致谢。

因为它是一个查询选择器,所以使用div.main a

content = $("div.main a");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM