|
|
@@ -2,6 +2,7 @@
|
|
|
const { chromium, firefox, webkit } = require('playwright')
|
|
|
const axios = require('axios').default
|
|
|
const Queue = require('queue')
|
|
|
+const { setTimeout } = require('timers/promises')
|
|
|
|
|
|
module.exports = async function (fastify, opts) {
|
|
|
fastify.post('/search', async function (request, reply) {
|
|
|
@@ -34,14 +35,23 @@ module.exports = async function (fastify, opts) {
|
|
|
// })
|
|
|
let bottom = 1
|
|
|
while (bottom > 0) {
|
|
|
- await page.evaluate(() => {
|
|
|
- window.scrollBy(0, 200)
|
|
|
- })
|
|
|
- await page.waitForTimeout(100)
|
|
|
+ while (bottom > 0) {
|
|
|
+ await page.evaluate(() => {
|
|
|
+ window.scrollBy(0, 200)
|
|
|
+ })
|
|
|
+ await page.waitForTimeout(100)
|
|
|
+ bottom = await page.evaluate(() => {
|
|
|
+ return (
|
|
|
+ document.documentElement.scrollHeight -
|
|
|
+ (window.innerHeight + document.documentElement.scrollTop)
|
|
|
+ )
|
|
|
+ })
|
|
|
+ console.log(bottom)
|
|
|
+ }
|
|
|
+ await page.waitForTimeout(1000)
|
|
|
bottom = await page.evaluate(() => {
|
|
|
return document.documentElement.scrollHeight - (window.innerHeight + document.documentElement.scrollTop)
|
|
|
})
|
|
|
- console.log(bottom)
|
|
|
}
|
|
|
await page.waitForTimeout(2000)
|
|
|
|
|
|
@@ -53,37 +63,42 @@ module.exports = async function (fastify, opts) {
|
|
|
const queue = new Queue({ results: [] })
|
|
|
items.forEach(item => {
|
|
|
queue.push(async () => {
|
|
|
- let atags = await item.locator('a').all()
|
|
|
- if (atags && atags[1]) {
|
|
|
- let text = await atags[1].innerText()
|
|
|
- let href = await atags[1].getAttribute('href')
|
|
|
- let match = /\/dp\/(.*?)\//.exec(href)
|
|
|
- let asin
|
|
|
- if (match && match[1]) {
|
|
|
- asin = match[1]
|
|
|
- }
|
|
|
+ for (let i = 0; i < 3; i++) {
|
|
|
+ try {
|
|
|
+ let atags = await item.locator('a').all()
|
|
|
+ if (atags && atags[1]) {
|
|
|
+ let text = await atags[1].innerText()
|
|
|
+ let href = await atags[1].getAttribute('href')
|
|
|
+ let match = /\/dp\/(.*?)\//.exec(href)
|
|
|
+ let asin
|
|
|
+ if (match && match[1]) {
|
|
|
+ asin = match[1]
|
|
|
+ }
|
|
|
|
|
|
- let rows = await item.locator('.a-row').all()
|
|
|
- let price = null
|
|
|
- if (rows.length > 1) {
|
|
|
- price = await rows[1].innerText()
|
|
|
- }
|
|
|
- console.log(asin + ' start')
|
|
|
- let sif = await axios.get('https://www.sif.com/api/search/asinFlowOverview', {
|
|
|
- params: {
|
|
|
- country: 'US',
|
|
|
- asin,
|
|
|
- _t: new Date().getTime()
|
|
|
+ let rows = await item.locator('.a-row').all()
|
|
|
+ let price = null
|
|
|
+ if (rows.length > 1) {
|
|
|
+ price = await rows[1].innerText()
|
|
|
+ }
|
|
|
+ console.log(asin + ' start')
|
|
|
+ let sif = await axios.get('https://www.sif.com/api/search/asinFlowOverview', {
|
|
|
+ params: {
|
|
|
+ country: 'US',
|
|
|
+ asin,
|
|
|
+ _t: new Date().getTime()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ console.log(asin + ' end')
|
|
|
+ return { text, href, price, asin, data: sif.data.data }
|
|
|
}
|
|
|
- })
|
|
|
- console.log(asin + ' end')
|
|
|
- return { text, href, price, asin, data: sif.data.data }
|
|
|
+ } catch (error) {}
|
|
|
+ await setTimeout(1000)
|
|
|
}
|
|
|
})
|
|
|
})
|
|
|
res = await new Promise((resolve, reject) => {
|
|
|
queue.start(() => {
|
|
|
- resolve(queue.results)
|
|
|
+ resolve(queue.results.filter(item => !!item))
|
|
|
})
|
|
|
})
|
|
|
await browser.close()
|