gpt4 book ai didi

javascript - 如何在 Node.js 中根据纬度和经度设置自定义位置?

转载 作者:行者123 更新时间:2023-12-01 02:51:01 25 4
gpt4 key购买 nike

在进行 Headless Chrome (Puppeteer) 搜索时,我需要能够根据纬度和经度设置位置:

const puppeteer = require('puppeteer');
var url="https://www.google.com/search?q=";
var keyword="hotels&num=100";
var urls;
var desktopUserAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";
const response=[];
var i=0;
var userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";

(async () =>
const browser = await puppeteer.launch({headless: false });
const page = await browser.newPage();
urls=url+keyword;
/*page.setViewport({
width: 1000,
height: 1000
});*/
page.setUserAgent(userAgent);
response[i]=await page.goto(urls);
console.log(await browser.version());
})();

最佳答案

您可以创建 CDPSession并使用 Chrome DevTools 协议(protocol)覆盖地理位置 Page.setGeolocationOverride :

const client = await page.target().createCDPSession();

await client.send('Emulation.setGeolocationOverride', {
accuracy: 100,
latitude: 24.42312,
longitude: 105.75868,
});

关于javascript - 如何在 Node.js 中根据纬度和经度设置自定义位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46968705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com