Javascript 刮伤<;p>;在<;h2>;木偶演员
我是新来的木偶演员,学习刮网页。网页的结构如下所示: 我要做的是清除Javascript 刮伤<;p>;在<;h2>;木偶演员,javascript,node.js,web-scraping,puppeteer,Javascript,Node.js,Web Scraping,Puppeteer,我是新来的木偶演员,学习刮网页。网页的结构如下所示: 我要做的是清除状态和Naam之间的所有标记。使用当前代码,我可以清除此页面上的所有标记。直到现在,我才尝试将状态之后的所有标记刮到Naam 我当前的代码: const puppeteer = require('puppeteer'); const plaatsengids = async (place) => { //Creates a Headless Browser Instance in the Background
状态
和Naam
之间的所有
标记。使用当前代码,我可以清除此页面上的所有
标记。直到现在,我才尝试将状态
之后的所有
标记刮到Naam
我当前的代码:
const puppeteer = require('puppeteer');
const plaatsengids = async (place) => {
//Creates a Headless Browser Instance in the Background
const browser = await puppeteer.launch();
//Creates a Page Instance, similar to creating a new Tab
const page = await browser.newPage();
//Navigate the page to url
await page.goto('https://plaatsengids.nl/'+place);
/* page.waitForSelector('.title').then(async function(){
const title = await page.$eval('.title', element => element.innerHTML);
})*/
//Finds the first element with the id 'hplogo' and returns the source attribute of that element
const Title = await page.$eval('.title', element => element.innerHTML);
const description = await page.$eval('.body p', element => element.innerHTML);
let content = await page.evaluate(() => {
let divs = [...document.querySelectorAll('.body p')];
return divs.map((div) => div.textContent.replace("- ",""));
});
//Closes the Browser Instance
await browser.close();
return content;
};
module.exports = plaatsengids;
有关网页如下:
您可以使用:
const puppeter=require('puppeter');
(异步函数main(){
试一试{
const browser=wait puppeter.launch();
const[page]=wait browser.pages();
等待页面。转到('https://www.plaatsengids.nl/Stein');
const段落=等待页面。评估(()=>{
const status=document.querySelector('h2[name=“status”]”);
const naam=document.querySelector('h2[name=“naam”]”);
return[…document.querySelectorAll('p')]
.filter(p=>p.compareDocumentPosition(status)&Node.DOCUMENT\u POSITION\u&&
p、 比较文档位置(naam)和节点。文档位置(以下)
.map(p=>p.innerText);
});
控制台日志(段落);
等待浏览器关闭();
}捕捉(错误){
控制台错误(err);
}
})();