一个响应返回我想要的数据,另一个响应(在不同的环境(firebase服务器)中)失败时出错,告诉我:‘你看起来像机器人’(我也从我的朋友那里得到了这个错误,lol)。
概述:
我用爪哇脚本中的傀儡手来在点击一个按钮后得到结果。问题是,尽管代码在我的本地机器上按预期工作。相同的代码在部署到firebase服务器(firebase云函数)时,对于相同的请求得到不同的结果:
无头铬浏览器中工艺的透视图:
点击一个按钮-->一个带有新数据的标签更改,我收集这些数据。单击按钮会触发一个请求,该请求返回json文件的响应,其中包含我希望存储在页面中的标签中的数据。
同样的请求发生在相同的环境(本地服务器和防火墙服务器)中,我设法看到使用傀儡机'on‘方法,并将其打印到控制台日志中:
page.on('requestfinished', request => console.info(` Finished request: ${request.url()}`));我还使用一种方法来查看我得到的响应,如下所示:
await page.on('response', async response =>{
try {
console.info((await response.json()));
} catch (error) {
console.error(error);
}
});正如我所说,在相同请求之后得到的json的结果是不同的。而在当地环境中,一切都井井有条。
对于相同的请求,我将收到带有错误消息的json。
{"success":false,"message":"You look like a robot."}你知道这是怎么发生的吗?怎么解决呢?
更新 #1
代码只在我没有将它部署到A服务器时才能工作,即使它是我的本地主机。只有当我在VSCode上运行它时(没有服务器函数包围它,),我才会得到我想要的响应
这意味着只有当代码没有被封装在服务器函数中时,它才能工作。当我使用服务器的时候,这个网站有一些如何‘知道’的东西。为什么会这样,还有什么办法可以避免呢?
更新 #2
我设法在我的本地主机上收到一个响应,使用傀儡-页面-代理包。每项请求一项如下:
const useProxy = require('puppeteer-page-proxy');
page.on('request', async request => {
await useProxy(request, 'https://116.80.49.253:3128');
});但是,当将它部署到firebase云功能时,我在firebase日志中得到了“崩溃”:

这样做的原因是什么,是否有可能“代理”我在防火墙云功能上没有启用?
更新 #3
所以,这一集的“探索”继续.
现在,就像我刚才说的,在更新#2中,我终于得到了一个正确的响应(对按一下按钮触发器的请求)
这种情况发生在我的本地主机上--> firebase serve --only functions
现在,在“另一方面”- firebase deploy --only functions
但是,阿拉斯!我得到的回应又是--“你看起来像个机器人”
唯一不同的是代码从哪里运行。
将它部署到我的本地主机->工作!✅
firebase serve --only functions将其部署到防火墙服务器-不起作用!❌
我正在尽我所能来描述这个问题
现在的问题是,我用木偶手控制他的网站,检测到一些方法,当我从本地主机使用它时,我使用的是一个驱动程序,但是我不知道如何使用它:)。
**好的,这是代码**->所有关于这个问题的答案。希望这能让我对我所面临的问题有所了解,也许,也许能帮助我解决这个问题。
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const puppeteer = require('puppeteer');
const useProxy = require('puppeteer-page-proxy');
admin.initializeApp()
exports.startPuppeteer = functions.runWith({memory:'1GB'}).https.onRequest((request, response) => {
console.log('hello world');
(async () => {
console.log('im in the functions');
const browser = await puppeteer.launch({headless:true})
const page = await browser.newPage()
// setting requests to use the proxy when requesting
await page.setRequestInterception(true);
page.on('request', async request => {
try{
if ( [
//'stylesheet',
'image',
'media',
'font',
].indexOf( request.resourceType() ) !== -1 ) {
request.abort();
} else {
request.continue();
await useProxy(request, 'https://116.80.49.253:3128');
};
}catch(e){
request.continue();
}
});
// getting and printing to the console the response, if its some kind of Json :
await page.on('response', async response =>{
try {
console.info((await response.json()));
console.succ( await response.json())
} catch (error) {
}
});
// going to the site that as the button that i want to be clicked on in order to fetch the result of the data the will follows the clicking:
await page.goto('https://thesitetobecontrel.com');
console.log('done?');
// setting the screen to a proper dimensions in order to click the button:
await page.setViewport({
width: 1200,
height: 800
});
await page.evaluate('window.scrollTo(0, 300)')
try {
// getting the text field object that need to be filled with the relevent data:
const [textFiled] = await page.$x('/html/body/div[1]/div[1]/div[1]/div/div[1]/center/div/input');
// getting the dropdown list object in order to pick the wanted option
const [listToSelcetOptionFrom] = await page.$x('//*[@id="content"]/div[1]/div[1]/div/div[1]/center/div/div/select');
// getting the button that need to be clicked after filling the text field, and picking an option from the list
const [btn] = await page.$x('//*[@id="content"]/div[1]/div[1]/div/button');
// selecting the desired option , with text :
await listToSelcetOptionFrom.select('optionText');
console.log('done?');
// filling the text field with the relevent info that need to be set before clicking the button :
await textFiled.type('someInfo');
await page.waitForTimeout(2000);
// clicking on the button with mouse
const box = await btn.boundingBox();
const x = box.x + (box.width/2);
const y = box.y + (box.height/2);
console.log(x, y);
page.mouse.move(x,y);
page.mouse.click(x,y)
// waiting for the result of the click
await page.waitForTimeout(7000);
console.info('wait over 7 secound...');
// fetcing, and sending the result
const [result] = await page.$x('//*[@id="content"]/div[1]/div[1]/div/div[2]/div');
// const txt = await result.evaluate.toString
let value = await page.evaluate(el => el.textContent, result);
console.log(value);
console.log('done?');
await browser.close();
const dic = {};
dic['status'] = 200;
dic['data'] = {"message": value};
response.send(dic);
} catch(err) {
console.error(err);
const dic = {};
dic['status'] = 200;
dic['data'] = {"message": 'Error'};
}
})});https://stackoverflow.com/questions/70848360
复制相似问题