index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use strict'
  2. const { chromium, firefox, webkit } = require('playwright')
  3. const axios = require('axios').default
  4. const Queue = require('queue')
  5. module.exports = async function (fastify, opts) {
  6. fastify.post('/search', async function (request, reply) {
  7. let response = await axios.post('https://www.sif.com/api/user/login?_t=' + new Date().getTime(), {
  8. phone: '18390881087',
  9. password: 'Gaole1401'
  10. })
  11. let token = response.headers['authorization']
  12. axios.defaults.headers.common['authorization'] = token
  13. console.log(token)
  14. const browser = await chromium.launch({ headless: true }) // Or 'firefox' or 'webkit'.
  15. const page = await browser.newPage()
  16. await page.goto(request.body.url, { timeout: 60000 })
  17. // await page.evaluate(async () => {
  18. // await new Promise((resolve, reject) => {
  19. // let i = setInterval(() => {
  20. // console.log(
  21. // (window.innerHeight, document.documentElement.scrollTop, document.documentElement.scrollHeight)
  22. // )
  23. // if (window.innerHeight + document.documentElement.scrollTop > document.documentElement.scrollHeight) {
  24. // clearInterval(i)
  25. // resolve()
  26. // } else {
  27. // window.scrollBy(0, 200)
  28. // }
  29. // }, 100)
  30. // })
  31. // })
  32. let bottom = 1
  33. while (bottom > 0) {
  34. await page.evaluate(() => {
  35. window.scrollBy(0, 200)
  36. })
  37. await page.waitForTimeout(100)
  38. bottom = await page.evaluate(() => {
  39. return document.documentElement.scrollHeight - (window.innerHeight + document.documentElement.scrollTop)
  40. })
  41. console.log(bottom)
  42. }
  43. await page.waitForTimeout(2000)
  44. //find a tag using page.locator
  45. const items = await page.locator('.p13n-desktop-grid .a-column').all()
  46. console.log(items.length)
  47. let res = []
  48. const queue = new Queue({ results: [] })
  49. items.forEach(item => {
  50. queue.push(async () => {
  51. let atags = await item.locator('a').all()
  52. if (atags && atags[1]) {
  53. let text = await atags[1].innerText()
  54. let href = await atags[1].getAttribute('href')
  55. let match = /\/dp\/(.*?)\//.exec(href)
  56. let asin
  57. if (match && match[1]) {
  58. asin = match[1]
  59. }
  60. let rows = await item.locator('.a-row').all()
  61. let price = null
  62. if (rows.length > 1) {
  63. price = await rows[1].innerText()
  64. }
  65. let sif = await axios.get('https://www.sif.com/api/search/asinFlowOverview', {
  66. params: {
  67. country: 'US',
  68. asin,
  69. _t: new Date().getTime()
  70. }
  71. })
  72. return { text, href, price, asin, data: sif.data.data }
  73. }
  74. })
  75. })
  76. res = await new Promise((resolve, reject) => {
  77. queue.start(() => {
  78. resolve(queue.results)
  79. })
  80. })
  81. await browser.close()
  82. return res
  83. await Promise.all(
  84. items.map(item => {
  85. return new Promise(async (resolve, reject) => {
  86. try {
  87. let atags = await item.locator('a').all()
  88. if (atags && atags[1]) {
  89. let text = await atags[1].innerText()
  90. let href = await atags[1].getAttribute('href')
  91. let match = /\/dp\/(.*?)\//.exec(href)
  92. let asin
  93. if (match && match[1]) {
  94. asin = match[1]
  95. }
  96. let rows = await item.locator('.a-row').all()
  97. let price = null
  98. if (rows.length > 1) {
  99. price = await rows[1].innerText()
  100. }
  101. let sif = await axios.get('https://www.sif.com/api/search/asinFlowOverview', {
  102. params: {
  103. country: 'US',
  104. asin,
  105. _t: new Date().getTime()
  106. }
  107. })
  108. res.push({ text, href, price, asin, data: sif.data.data })
  109. }
  110. resolve()
  111. } catch (e) {
  112. reject()
  113. }
  114. })
  115. })
  116. )
  117. await browser.close()
  118. return res
  119. })
  120. }