如何解决Nodejs无法通过请求承诺请求网址
我无法使用以下代码从 OpenTable 网站获取 https。但是,当我尝试从其他站点获取数据时,例如Instagram 使用相同的代码,它可以工作。因为我刚刚开始学习使用 node js 进行网页抓取,你能告诉我可能是什么问题吗?
const request = require('request-promise');
(async () => {
const BASE_URL = 'https://www.opentable.co.uk/s?dateTime=2021-05-30T19%3A00%3A00&covers=2&latitude=51.525225&longitude=-0.079615'
let response = await request(BASE_URL);
let $ = cheerio.load(response);
console.log(response);
})();
解决方法
很可能您的代码不起作用,因为 JavaScript 用于在网站上构建 DOM。或者该站点正在使用防刮保护。无论如何,我会建议你像Puppeteer:
const puppeteer = require("puppeteer-extra");
const StealthPlugin = require("puppeteer-extra-plugin-stealth");
puppeteer.use(StealthPlugin());
async function scrapeOpenTable() {
const BASE_URL =
"https://www.opentable.co.uk/s?dateTime=2021-05-30T19%3A00%3A00&covers=2&latitude=51.525225&longitude=-0.079615";
const browser = await puppeteer.launch({
headless: false,args: ["--no-sandbox","--disable-setuid-sandbox"],});
const page = await browser.newPage();
await page.goto(BASE_URL);
// More commands here...
/* You may view the docs at:
https://pptr.dev/
And more magic at:
https://www.npmjs.com/package/puppeteer
Github:
https://github.com/puppeteer/puppeteer
*/
// await browser.close();
}
scrapeOpenTable();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。