可以使用Puppeteer -Thingiverse单击链接单击链接
我正在尝试自动化在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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够让它上班。
选择器为:
a [class^=“ thit file__download”]
puppeteer是:
const puppeteer = require('puppeteer-extra');
在
等待页面之前。 goto()
我始终建议设置视口:设置之后,更改
等待Page.goto()
具有waituntil
选项:接下来,这是一个非常重要的部分。您必须执行所谓的
waitforselector()
或waitforfunction()
。我在
const响应
之后添加了这两个代码行:接下来,获取
buttons
。对于我的测试,我只是抓住按钮
href
。最后,请勿检查此变量的
.length
。在这种情况下,我们只是返回href
值,即String
。您将获得Promise
的ElementHandle
时,您将尝试获得按钮:如果您更改该
page。$
为页面。$$
,您将获得array< elementHandle>
的Promise
,并且将能够使用.length
那里。希望这会有所帮助,如果您无法弄清楚,如果我有时间使它看起来更好,我可以稍后发布我的全部资源。
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:After that is set, change the
await page.goto()
to have awaitUntil
option:Next, this is a very important part. You have to do what is called
waitForSelector()
orwaitForFunction()
.I added both of these lines of code after the
const response
:Next, get the
buttons
. For my testing I just grabbed thebutton
href
.Lastly, do not check the
.length
of this variable. In this case we are just returning thehref
value which is astring
. You will get aPromise
of anElementHandle
when you try getting just the button:Now if you change that
page.$
to bepage.$$
, you will be getting aPromise
of anArray<ElementHandle>
, and will be able to use.length
there.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.