| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- 'use strict'
- const { chromium, firefox, webkit } = require('playwright')
- const axios = require('axios').default
- const Queue = require('queue')
- module.exports = async function (fastify, opts) {
- fastify.post('/search', async function (request, reply) {
- let response = await axios.post('https://www.sif.com/api/user/login?_t=' + new Date().getTime(), {
- phone: '18390881087',
- password: 'Gaole1401'
- })
- let token = response.headers['authorization']
- axios.defaults.headers.common['authorization'] = token
- console.log(token)
- const browser = await chromium.launch({ headless: true }) // Or 'firefox' or 'webkit'.
- const page = await browser.newPage()
- await page.goto(request.body.url, { timeout: 60000 })
- // await page.evaluate(async () => {
- // await new Promise((resolve, reject) => {
- // let i = setInterval(() => {
- // console.log(
- // (window.innerHeight, document.documentElement.scrollTop, document.documentElement.scrollHeight)
- // )
- // if (window.innerHeight + document.documentElement.scrollTop > document.documentElement.scrollHeight) {
- // clearInterval(i)
- // resolve()
- // } else {
- // window.scrollBy(0, 200)
- // }
- // }, 100)
- // })
- // })
- let bottom = 1
- 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(2000)
- //find a tag using page.locator
- const items = await page.locator('.p13n-desktop-grid .a-column').all()
- console.log(items.length)
- let res = []
- 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]
- }
- let rows = await item.locator('.a-row').all()
- let price = null
- if (rows.length > 1) {
- price = await rows[1].innerText()
- }
- let sif = await axios.get('https://www.sif.com/api/search/asinFlowOverview', {
- params: {
- country: 'US',
- asin,
- _t: new Date().getTime()
- }
- })
- return { text, href, price, asin, data: sif.data.data }
- }
- })
- })
- res = await new Promise((resolve, reject) => {
- queue.start(() => {
- resolve(queue.results)
- })
- })
- await browser.close()
- return res
- await Promise.all(
- items.map(item => {
- return new Promise(async (resolve, reject) => {
- 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()
- }
- let sif = await axios.get('https://www.sif.com/api/search/asinFlowOverview', {
- params: {
- country: 'US',
- asin,
- _t: new Date().getTime()
- }
- })
- res.push({ text, href, price, asin, data: sif.data.data })
- }
- resolve()
- } catch (e) {
- reject()
- }
- })
- })
- )
- await browser.close()
- return res
- })
- }
|