可以使用Puppeteer -Thingiverse单击链接单击链接

发布于 2025-02-13 20:05:27 字数 1162 浏览 0 评论 0原文

我正在尝试自动化在Thingiverse上下载多个文件的自动化。我随机选择一个对象。但是我很难找到所需的链接,单击然后下载。有人遇到过我可以得到一些帮助吗?

我尝试了其他几种变体。

import puppeteer from 'puppeteer';

async function main() {
    const browser = await puppeteer.launch({
        headless: true,
    });

    const page = await browser.newPage();
    const response = await page.goto('https://www.thingiverse.com/thing:2033856/files');
    const buttons = await page.$x(`//a[contains(text(), 'Download')]`);
    if(buttons.length > 0){
        console.log(buttons.length);
    }  else {
        console.log('no buttons'); 
    }
    await wait(5000);
    await browser.close();
    return 'Finish';
}
async function wait(time: number) {
    return new Promise(function (resolve) {
        setTimeout(resolve, time);
    });
}

function start() {
    main()
        .then((test) => console.log('DONE'))
        .catch((reason) => console.log('Error: ', reason));
}

start();

下载页面

代码

I'm trying to automate away downloading multiple files on thingiverse. I choose an object at random. But I'm having a hard time locating the link I need, clicking and then downloading. Has someone run into this before can I get some help?

I've tried several other variations.

import puppeteer from 'puppeteer';

async function main() {
    const browser = await puppeteer.launch({
        headless: true,
    });

    const page = await browser.newPage();
    const response = await page.goto('https://www.thingiverse.com/thing:2033856/files');
    const buttons = await page.$x(`//a[contains(text(), 'Download')]`);
    if(buttons.length > 0){
        console.log(buttons.length);
    }  else {
        console.log('no buttons'); 
    }
    await wait(5000);
    await browser.close();
    return 'Finish';
}
async function wait(time: number) {
    return new Promise(function (resolve) {
        setTimeout(resolve, time);
    });
}

function start() {
    main()
        .then((test) => console.log('DONE'))
        .catch((reason) => console.log('Error: ', reason));
}

start();

Download Page

Code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浸婚纱 2025-02-20 20:05:27

我能够让它上班。
选择器为:a [class^=“ thit file__download”]

puppeteer是:const puppeteer = require('puppeteer-extra');

等待页面之前。 goto()我始终建议设置视口:

await page.setViewport({width: 1920, height: 720});

设置之后,更改等待Page.goto()具有waituntil选项:

const response = await page.goto('https://www.thingiverse.com/thing:2033856/files', { waitUntil: 'networkidle0' }); // wait until page load

接下来,这是一个非常重要的部分。您必须执行所谓的waitforselector()waitforfunction()

我在const响应之后添加了这两个代码行:

await page.waitForSelector('a[class^="ThingFile__download"]', {visible: true})

await page.waitForFunction("document.querySelector('a[class^=\"ThingFile__download\"]') && document.querySelector('a[class^=\"ThingFile__download\"]').clientHeight != 0");

接下来,获取buttons。对于我的测试,我只是抓住按钮 href

const buttons = await page.$eval('a[class^="ThingFile__download"]', anchor => anchor.getAttribute('href'));

最后,请勿检查此变量的.length。在这种情况下,我们只是返回href值,即String。您将获得PromiseElementHandle时,您将尝试获得按钮:

const button = await page.$('a[class^="ThingFile__download"]');
console.log(button)
if (button) { ... }

如果您更改该page。$页面。$$,您将获得array< elementHandle>Promise,并且将能够使用.length那里。

const buttonsAll = await page.$('a[class^="ThingFile__download"]');
console.log(buttonsAll)
if (buttons.length > 0) { ... }

希望这会有所帮助,如果您无法弄清楚,如果我有时间使它看起来更好,我可以稍后发布我的全部资源。

I was able to get it to work.
The selector is: a[class^="ThingFile__download"]

Puppeteer is: const puppeteer = require('puppeteer-extra');

Before the await page.goto() I always recommend setting the viewport:

await page.setViewport({width: 1920, height: 720});

After that is set, change the await page.goto() to have a waitUntil option:

const response = await page.goto('https://www.thingiverse.com/thing:2033856/files', { waitUntil: 'networkidle0' }); // wait until page load

Next, this is a very important part. You have to do what is called waitForSelector() or waitForFunction().

I added both of these lines of code after the const response:

await page.waitForSelector('a[class^="ThingFile__download"]', {visible: true})

await page.waitForFunction("document.querySelector('a[class^=\"ThingFile__download\"]') && document.querySelector('a[class^=\"ThingFile__download\"]').clientHeight != 0");

Next, get the buttons. For my testing I just grabbed the button href.

const buttons = await page.$eval('a[class^="ThingFile__download"]', anchor => anchor.getAttribute('href'));

Lastly, do not check the .length of this variable. In this case we are just returning the href value which is a string. You will get a Promise of an ElementHandle when you try getting just the button:

const button = await page.$('a[class^="ThingFile__download"]');
console.log(button)
if (button) { ... }

Now if you change that page.$ to be page.$$, you will be getting a Promise of an Array<ElementHandle>, and will be able to use .length there.

const buttonsAll = await page.$('a[class^="ThingFile__download"]');
console.log(buttonsAll)
if (buttons.length > 0) { ... }

Hopefully this helps, and if you can't figure it out I can post my full source later if I have time to make it look better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文