使用 puppeteer 在谷歌图片上获取 img src [英] get img src on google images with puppeteer
本文介绍了使用 puppeteer 在谷歌图片上获取 img src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做一个小脚本,我希望它从谷歌照片上的第一张图片中获取 src,我尝试了很多东西,但我无法获取 src 并将其保存到变量中.这是目前为止的脚本,如果您能帮助我,我将不胜感激.
Im doing a small script, and i want it to get a src from the first image on google photos, i have tried so many things, but i havent been able to get the src and save it into a variable. This is the script so far, i will be very grateful if you help me.
const puppeteer = require('puppeteer');
let imgSrc
(async () => {
const browser = await puppeteer.launch({
headless: false,
});
const page = await browser.newPage();
await page.setViewport({
width:1920,
height:1080,
isMobile: false
})
await page.goto('https://www.google.com/imghp');
await page.waitForXPath('/html/body/div[2]/div[2]/div[2]/form/div[2]/div[1]/div[1]/div/div[2]/input')
await page.click('#sbtc > div > div.a4bIc > input')
await page.type('#sbtc > div > div.a4bIc > input', 'Tiras Glucomet Freestyle Optimun 50 unidades y 100 unidades')
await page.click('#sbtc > button > div > span > svg')
await page.waitForXPath('/html/body/div[2]/c-wiz/div[3]/div[1]/div/div/div/div/div[1]/div[1]/div[1]/a[1]/div[1]/img')
await page.click('#islrg > div.islrc > div:nth-child(1) > a.wXeWr.islib.nfEiy.mM5pbd > div.bRMDJf.islir > img')
await page.waitForXPath('/html/body/div[2]/c-wiz/div[3]/div[2]/div[3]/div/div/div[3]/div[2]/c-wiz/div[1]/div[1]/div/div[2]/a/img')
const data = await page.evaluate(() => {
let src = document.querySelector('#Sva75c > div > div > div.pxAole > div.tvh9oe.BIB1wf > c-wiz > div.OUZ5W > div.zjoqD > div > div.v4dQwb > a > img').getAttribute('src')
imgSrc = src
})
console.log(imgSrc)
})();
推荐答案
page.evaluate()
函数参数 оf 中的代码在浏览器上下文中执行,因此它没有访问 puppeteer 的变量上下文包括 imgSrc
.您需要传输数据,然后将其分配给变量:
The code in function argument оf page.evaluate()
is executed in browser context so it has not acces to variables of the puppeteer context including imgSrc
. You need to transfer the data and then assign it to the variable:
imgSrc = await page.evaluate(() => {
let src = document.querySelector('#Sva75c > div > div > div.pxAole > div.tvh9oe.BIB1wf > c-wiz > div.OUZ5W > div.zjoqD > div > div.v4dQwb > a > img').getAttribute('src')
return src;
})
console.log(imgSrc)
这篇关于使用 puppeteer 在谷歌图片上获取 img src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文