test.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const { chromium, firefox, webkit } = require('playwright')
  2. const axios = require('axios').default
  3. const fs = require('fs')
  4. ;(async () => {
  5. let response = await axios.post('https://www.deeperloop.com/api/user/login?_t=' + new Date().getTime(), {
  6. phone: '18390881087',
  7. password: 'Gaole1401'
  8. })
  9. let token = response.headers['authorization']
  10. axios.defaults.headers.common['authorization'] = token
  11. console.log(token)
  12. const browser = await chromium.launch({ headless: true }) // Or 'firefox' or 'webkit'.
  13. const page = await browser.newPage()
  14. await page.goto('https://www.amazon.com/gp/bestsellers/pet-supplies/2975292011/ref=pd_zg_hrsr_pet-supplies')
  15. // await page.evaluate(async () => {
  16. // await new Promise((resolve, reject) => {
  17. // let i = setInterval(() => {
  18. // console.log(
  19. // (window.innerHeight, document.documentElement.scrollTop, document.documentElement.scrollHeight)
  20. // )
  21. // if (window.innerHeight + document.documentElement.scrollTop > document.documentElement.scrollHeight) {
  22. // clearInterval(i)
  23. // resolve()
  24. // } else {
  25. // window.scrollBy(0, 200)
  26. // }
  27. // }, 100)
  28. // })
  29. // })
  30. let bottom = 1
  31. while (bottom > 0) {
  32. await page.evaluate(() => {
  33. window.scrollBy(0, 200)
  34. })
  35. await page.waitForTimeout(100)
  36. bottom = await page.evaluate(() => {
  37. return document.documentElement.scrollHeight - (window.innerHeight + document.documentElement.scrollTop)
  38. })
  39. console.log(bottom)
  40. }
  41. await page.waitForTimeout(2000)
  42. //find a tag using page.locator
  43. const items = await page.locator('.p13n-desktop-grid .a-column').all()
  44. console.log(items.length)
  45. const res = []
  46. for (let item of items) {
  47. let atags = await item.locator('a').all()
  48. if (atags && atags[1]) {
  49. let text = await atags[1].innerText()
  50. let href = await atags[1].getAttribute('href')
  51. let match = /\/dp\/(.*?)\//.exec(href)
  52. let asin
  53. if (match && match[1]) {
  54. asin = match[1]
  55. }
  56. let price = (await item.locator('.a-row').last().allInnerTexts()).join()
  57. let sif = await axios.get('https://www.deeperloop.com/api/search/asinFlowOverview', {
  58. params: {
  59. country: 'US',
  60. asin,
  61. _t: new Date().getTime()
  62. }
  63. })
  64. res.push({ text, href, price, asin, data: sif.data.data })
  65. }
  66. }
  67. fs.writeFileSync('res.json', JSON.stringify(res, null, 4))
  68. await browser.close()
  69. })()