简体   繁体   English

如何获取 HTML 元素但使用 querySelector 排除所有子元素

[英]How to get the HTML element but exclude all child elements with querySelector

Currently using document.querySelector with puppeteer to retrieve the video links from a Tiktok account's HTML code and am having issues retrieving exactly what I need目前使用带有 puppeteer 的 document.querySelector 从 Tiktok 帐户的 HTML 代码中检索视频链接,并且在检索我需要的内容时遇到问题

With this code:使用此代码:

const grabURLs = await page.evaluate(() => {
    const pgTag = document.querySelector('.tiktok-1qb12g8-DivThreeColumnContainer.eegew6e2 div div div div div')
    return pgTag.innerHTML;
})

console.log(grabURLs)

I receive not only the href that I need but also all of the child elements below that, how do I limit it so the only innerHTML I receive is the first child?我不仅收到了我需要的 href,还收到了下面的所有子元素,我该如何限制它,所以我收到的唯一 innerHTML 是第一个子元素?

Any help would be greatly appreciated thank you!任何帮助将不胜感激,谢谢!

You just need to do a quick search of the page for all of the URLs that point to videos.您只需在页面上快速搜索所有指向视频的 URL。

Here's how to do it on Tiktok:以下是在 Tiktok 上的操作方法:

var videos = document.querySelectorAll("a[href*='/video/']");

You can use the firstChild property.您可以使用firstChild属性。

So, the code becomes:因此,代码变为:

const grabURLs = await page.evaluate(() => {
    const pgTag = document.querySelector('.tiktok-1qb12g8-DivThreeColumnContainer.eegew6e2 div div div div div')
    return pgTag.firstChild.innerHTML;
})

console.log(grabURLs)

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

相关问题 如何使用puppeteer获取html元素的所有子元素值 - How to get all the child elements values of an html element using puppeteer 如何在 JavaScript 中使用 querySelector 选择具有特定类的所有子元素,而不考虑它们在 HTML 中出现的顺序? - How can I select all child elements with a certain class using querySelector in JavaScript regardless of the order they appear in HTML? AngularJS-如何通过q​​uerySelector获取所有元素并禁用链接 - AngularJS - how to get all elements by querySelector and disable links vuejs 中如何获取 querySelector 元素? - How in vuejs get querySelector element? 量角器如何从父元素中获取所有子元素 - Protractor how to get all child elements from parent element 使用 querySelector 获取包含类的所有元素 - Get all elements containing a class with querySelector 对子元素使用 querySelector - Using querySelector for child elements 如何获得元素的className,Id和nth:child(n)以便使其成为稳定的好querySelector? - How to get element's className, Id, and nth:child(n) in order to be a good stable querySelector? 您将如何使用querySelector获取此元素? - How would you use querySelector to get this element? 如何使用querySelector获取元素的value属性 - How to get the value property of an element using querySelector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM