使axios等待重定向

问题描述 投票:0回答:1

我正在尝试使用/ search /从地图上从Google地图中抓取数据。

[当我自己搜索时,我将其录音:'https://www.google.com/maps/search/new约克'

然后,我被重定向到该URL:'https://www.google.com/maps/place/New+York,+%C3%89tat+de+New+York,+%C3%89tats-Unis/@40.6974881,-73.979681,10z/data=!3m1!4b1!4m5!3m4!1s0x89c24fa5d33f083b:0xc80b8f06e177fe62!8m2!3d40.7127753!4d-74.0059728'

我无法使用axios重现此行为。我猜想异步/等待也许有什么用,但是从那以后我没有找到任何解决方案。

这是我的代码:

const axios = require('axios');
const cheerio = require('cheerio');

var map = 'www.google.com/maps/search/';

axios.get(map + 'New York')
    .then(response => {
        let getData = html => {
            coor = [];
            v= -1;
            const $ = cheerio.load(html);
            $('.widget-pane-content scrollable-y').each((i, elem) => {
                coor.push({
                    adress : $(elem).find('span.widget-pane-link').text(),
                });
            });
            console.log(coor);
            console.log(coor.length);
        }
        getData(response.coor);
    })
    .catch(error => {console.log(error);})

执行文件时,出现此错误:

'Error: Request failed with status code 400'

如果您有解决我问题的线索,谢谢分享!

javascript node.js axios cheerio
1个回答
0
投票

查看Selenium或Cypress.js(硒的包装)之类的工具

(搜索“端到端测试”或“自动浏览器”)

不幸的是,无法使用Axios之类的工具来完成此操作。 Google Maps不返回redirect响应,而是使用JavaScript重新加载页面。

以柏树为例:

cy.visit("https://www.google.com/maps/search/new york");
cy.wait(2000);  // sit for 2 seconds
cy.get('#thing-im-looking-for')
© www.soinside.com 2019 - 2024. All rights reserved.