我正在使用"puppeteer": "^1.16.0",
测试我的网站是否可以承受代理服务器。
我开发了一个简短的木偶脚本,该脚本可以处理一系列代理,而且我的网站的响应应为:
在示例脚本下面找到:
function shuffle(dataObjArr) {
let res = dataObjArr.sort(() => Math.random() - 0.5);
return res
}
/**
* Create random number between start and end value
* @param {*} start
* @param {*} end
*/
function randomNumberBetween(start, end) {
return Math.floor(Math.random() * end) + start
}
/**
* get 10 proxies from a list
*/
function getProxies() {
// ...
return proxies
}
async function main() {
let issuerUrls = JSON.parse('[{"id":62,"name":"Product 1","ecomUrl":"/product/252","createdAt":"2019-05-25T07:51:49.000Z","updatedAt":"2019-05-25T07:51:49.000Z"}, {"id":15,"name":"Product 2","ecomUrl":"/product/251","createdAt":"2019-05-25T07:51:49.000Z","updatedAt":"2019-05-25T07:51:49.000Z"}, {"id":15,"name":"Product 2","ecomUrl":"/product/251","createdAt":"2019-05-25T07:51:49.000Z","updatedAt":"2019-05-25T07:51:49.000Z"}, {"id":15,"name":"Product 2","ecomUrl":"/product/251","createdAt":"2019-05-25T07:51:49.000Z","updatedAt":"2019-05-25T07:51:49.000Z"}]')
let issuerUrlsShuffled = shuffle(issuerUrls)
let BASE_URL = "https://www.webscraper.io/test-sites/e-commerce/allinone"
let p = await getProxies();
let proxies = [].concat(...p)
let shuffledProxies = shuffle(proxies)
let randNumbBool = true
let i = 0
let randNumb = 0
// for (let i = 0; i < issuerUrlsShuffled.length; i++) {
while (i < issuerUrlsShuffled.length) {
if (randNumbBool) {
randNumb = randomNumberBetween(0, shuffledProxies.length)
}
let proxy = '--proxy-server=' + shuffledProxies[randNumb].ipAddress + ":" + shuffledProxies[randNumb].port
await puppeteer.launch({
headless: false,
args: ["--disable-notifications", proxy] // --proxy-server=127.0.0.1:9876
}).then(async browser => {
const page = await browser.newPage()
await page.setViewport({
width: 800,
height: 600
})
try {
let URL = BASE_URL + issuerUrlsShuffled[i].ecomUrl;
await page.goto(URL)
page.waitForNavigation({
timeout: 0,
waitUntil: 'domcontentloaded'
});
await page.exposeFunction('formatDate', (date) =>
moment(date, 'M/DD/YYYY').toDate()
);
const data = await page.evaluate(async () => {
const priceData = []
let date = "9/23/2016" // this is needed for testing purposes only!!!
priceData.push({
price_date: await window.formatDate(date)
})
return priceData
})
// show data
// should be emtpy!!!
console.log(JSON.stringify(data, null, 2))
await page.waitFor(3000)
await browser.close()
randNumbBool = false
++i // next step in list
} catch (error) {
randNumbBool = true // get another random proxy
console.log(error)
}
})
}
}
main()
当我的网站对浏览器测试回应以上图片时,我的脚本停止运行,并且不再执行其他代理进行测试。
如何捕获错误ERR_CONNECTION_RESET
,并仍然运行我的脚本以完成所有可用代理的测试。
提前谢谢!