簡體   English   中英

通過axios獲取html頁面返回錯誤301/302

[英]Fetch html page via axios return error 301/302

我想獲取頁面 HTML,但每次嘗試獲取 HTTP 或 HTTPS 時,我都會收到錯誤 301 或 302 的響應。

已經嘗試添加代理 http 和 socks5 都沒有幫助

axios("https://www.macys.com", {
  method: "GET",
  maxRedirects: 1,
  headers: {
    Accept: "*/*",
    Connection: "keep-alive",
    "User-Agent":
      "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0",
  },
});

錯誤:

Error [ERR_FR_TOO_MANY_REDIRECTS]: Maximum number of redirects exceeded

如果我將最大重定向設置為 0

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "AxiosError: Request failed with status code 302".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

如果我將最大重定向設置為 1000 它只是無限加載

郵遞員的一個有趣事實,它工作正常

對於面臨同樣問題的人來說,這可能不是最好的方法,但我設法用puppeteer解決了它

const browser = await puppeteer.launch({
      headless: true,
      args: ["--no-sandbox", "--disable-setuid-sandbox"],
    });
    const page = await browser.newPage();
    await page.goto(url, {
      waitUntil: "domcontentloaded",
    });
    console.log(await page.content()); //our html
    await browser.close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM