我正在玩 puppeteer,以了解一些有关浏览器中自动化的知识。我想打开可见的铬浏览器,这样就不用无头了。我将启动选项设置为false,但它仍然无法打开 Chromium。
我尝试不使用沙箱参数,我什至--disable-extensions对参数中的进行了反标记,但没有任何帮助。
终端中没有错误,只是无法启动。
这是我的代码:
const puppeteer = require ("puppeteer");
async () => {
const browser = await puppeteer.launch({ headless: false });
const page = browser.newPage();
await page.goto("https://google.de");
await browser.close();
};
Run Code Online (Sandbox Code Playgroud)
知道为什么 chrome 不打开吗?也没有关于错误的日志...
我正在尝试使用 Puppeteer 和 tor 包 ( apt install tor)在 TOR 网络中抓取一些网页。可能是由于 TOR 连接的性质,有时我会超时。另外,我是 JavaScript 异步编程的新手。
通常我有一个像这样的 try-catch-construct:
await Promise.all([
page.goto(url),
page.waitForNavigation({
waitUntil: 'domcontentloaded'
}),
]).catch((err) => { logMyErrors(err, true); });
Run Code Online (Sandbox Code Playgroud)
或者
let langMenu = await page.waitForXPath('//*[contains(@class, ".customer_name")]/ancestor::li').catch((err) => { logMyErrors(err, true); });
Run Code Online (Sandbox Code Playgroud)
但我认为通常一次或多次重试将有助于最终获得所需的资源。是否有实施重试的最佳实践?
我需要让我的 api 能够更快地抓取 URL 列表。现在,我一次转到每一页并将数据添加到数组中。我需要一次打开多个链接并将从中获取的数据添加到同一个数组中。
这是我的代码:
var videos = [];
for(var i = 0; i < profile.videoLinks.length; i++){
await page.goto(profile.videoLinks[i].video, {
// waitUntil: 'load'
});
await page.waitForSelector('.music-info')
var vidInfo = await page.evaluate(() => {
const vidTitle = document.querySelector('.video-meta-title').innerText;
const musicInfo = document.querySelector('.music-info').innerText;
const musicLink = document.querySelector('.music-info a').href;
const likes = document.querySelector('.like-text').innerText;
const comments = document.querySelector('.comment-text').innerText;
return {
vidTitle,
musicInfo,
musicLink,
likes,
comments
}
})
videos.push(vidInfo);
Run Code Online (Sandbox Code Playgroud)
现在,我的链接数组位于profile.videoLinks[ ].video. 我应该将数组分成两半然后对每个数组使用评估函数吗?
<table><tr><td>firstContent</td><td>secondContent</td></tr></table>
Run Code Online (Sandbox Code Playgroud)
使用 puppeteer 查询此表page.$evalI 检索firstContent。我将如何检索secondContent?
const value = await page.$eval('table tr td', el => { return el.innerHTML });
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 puppeteer 来:
ul从列表创建数组我尝试使用多种方法来做到这一点,page.evaluate但我的数组似乎总是空的。有人可以告诉我我在这里做错了什么吗?
<ul>
<li><button>Connect</button></li>
<li><button>Wait</button></li>
<li><button>Connect</button></li>
<li><button>Connect</button></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
小路:puppeteer.js
const buttons = await page.evaluate(() =>
Array.from(document.querySelectorAll("ul > li button"))
);
for (let button of buttons) {
if (button.innerText === "Connect") {
button.click();
document
.querySelector(
"div.artdeco-modal"
)
.click();
}
}
Run Code Online (Sandbox Code Playgroud) 我在 Try Puppeteer 中使用此代码:
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.barchart.com/futures/quotes/ESM19/interactive-chart/fullscreen');
const linkHandlers = await page.$x("//li[contains(text(), '1D')]");
if (linkHandlers.length > 0) {
await linkHandlers[0].click();
} else {
throw new Error("Link not found");
}
await page.$eval('input[name="fieldInput"]', el => el.value = '1');
console.log(await page.content())
// const text = page.evaluate(() => document.querySelector('rect'))
// text.then((r) => {console.log(r[0])})
await page.screenshot({path: 'screenshot.png'});
await browser.close();
Run Code Online (Sandbox Code Playgroud)
Chrome 浏览器中加载的同一页面显示了指示价格变动的条形图,但在 Puppeteer 中获得的屏幕截图中,图表是空的。
还page.content()给出了一个与我在 Chrome 中检查元素时看到的完全不同的 html。
我得到的href元素a
const hrefs = await page.evaluate(() =>
Array.from(document.body.querySelectorAll('a'), ({ href }) => href));
Run Code Online (Sandbox Code Playgroud)
但是当我尝试获取aria-label或data-xx获取div元素时,此方法不起作用。
这是为什么?我如何获取aria-label元素data-xx的属性div?
超文本标记语言
<div class="test" arial-label="something" data-all="something">
</div>
Run Code Online (Sandbox Code Playgroud) javascript ×7
node.js ×7
puppeteer ×7
web-scraping ×2
async-await ×1
chromium ×1
headless ×1
tor ×1