javascript 如何用Puppeteer Js从www.example.com下载历史数据(csv格式)investing.com?

r7knjye2  于 2023-02-18  发布在  Java
关注(0)|答案(1)|浏览(131)

我试着用这段代码从investing.com下载csv格式的历史数据。

//collector.mjs
import puppeteer from "puppeteer";
import path from "path";

(async ()=>{
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.setUserAgent('Chrome/105.0.0.0');

  await page.goto("https://www.investing.com/equities/tesla-motors-historical-data", {
    waitUntil: "networkidle2",
  });

  const client = await page.target().createCDPSession();
  await client .send('Page.setDownloadBehavior', {
    behavior: 'allow',
    downloadPath: path.resolve("./csvData"),
  });

  await page.evaluate(()=>{
    document.querySelector("span[class='download-data_text__Myrn3']").click();
  });

  await browser.close();
})();

我得到的所有时间是“TSLA历史数据.csv.crdownload”文件(而不是)。

那么,如何得到这个(下图),而不是在我的文件夹csvData?

rt4zxlrg

rt4zxlrg1#

使用页面.setViewport()。

//collector.mjs
import puppeteer from "puppeteer";
import path from "path";

(async ()=>{
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.setUserAgent('Chrome/105.0.0.0');

  await page.setViewport({
    width: 1920,
    height: 1080
  });

  await page.goto("https://www.investing.com/equities/tesla-motors-historical-data", {
    waitUntil: "networkidle2",
  });

  const client = await page.target().createCDPSession();
  await client .send('Browser.setDownloadBehavior', {
    behavior: 'allow',
    downloadPath: path.resolve("./csvData"),
  });

  await page.click('.download-data_text__Myrn3');

  await browser.close();
})();

好了,

相关问题