这是抓取网站的 GET 函数
export async function GET(req) {
const itemNumber = req.url.split("scrape_costco/")[1];
console.log("Item #: ", itemNumber);
try {
console.log("scraping costco")
const res = await fetch(
`https://www.costco.com/CatalogSearch?dept=All&keyword=${itemNumber}`
);
// log the response url
console.log(res.url);
// console.log(res.headers)
console.log(req.headers)
const html = await res.text();
const dom = new JSDOM(html);
const document = dom.window.document;
const productName = document.querySelector(".description a")?.textContent;
console.log(productName);
if (!productName) throw new Error("Product not found");
return NextResponse.json(productName.trim());
} catch (error) {
return NextResponse.json(console.error(error));
}
}
在本地运行服务器可以很好地返回产品名称,但是一旦我部署到 vercel,该函数就无法运行。在后端,我得到一个状态代码 500,在客户端,它得到一个 504 代码,我也得到这个错误
488-8986c8e24f6e0c60.js:1 SyntaxError: Unexpected token 'A', "An error o"... is not valid JSON
我尝试过手动添加标头,也尝试创建一个快速应用程序来处理后端,但没有任何效果