[英]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.