我正在使用 Nest.js模塊與 Puppeteer 一起抓取網站。
該刮的作品我第一次呼叫GET方法,但它不會后再次刮。這意味著我必須為每次抓取都重新啟動服務器,這并不理想。
抓取代碼(簡體):
@Injectable()
export class AppService {
constructor(@InjectBrowser() private readonly browser: Browser) {}
async getFoodoraRestaurantMenus(url: string): Promise<string> {
console.log("Calling method");
const page = await this.browser.newPage();
console.log('Never gets here the second time');
await page.goto(url);
const content = await page.evaluate(() => {});
await this.browser.close();
return JSON.stringify(content, null, 4);
}
}
第二次打電話時,getFoodoraRestaurantMenus我永遠不會看到第二個控制臺日志,因此永遠不會打開新頁面。我不確定我是否需要做更多的事情browser.close(),這就是我所能找到的。
我不知道為什么每次要執行刮擦時都需要重新啟動服務器。非常感謝我能得到的所有幫助。
謝謝。
uj5u.com熱心網友回復:
如果您只想釋放記憶體并重用相同的瀏覽器,您可以嘗試只關閉頁面而不是瀏覽器:
await page.close();
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/341175.html
上一篇:IndexError:如果條件匹配,則在ifelse中列出超出范圍的索引
下一篇:如何在WPF中首先更新UI?
