在操纵up站点内获取数据

时间:2019-08-22 20:02:30

标签: node.js web-crawler puppeteer fetch-api

从外部服务获取数据时出现奇怪的问题。我有一个简单的脚本可以从API打印一些数据:

fetch("http://sip.ztm.kielce.pl/Home/CNR_GetVehicles?r=0W&d=&nb=", {"credentials":"include","headers":{"accept":"application/xml, text/xml, */*; q=0.01","accept-language":"pl;q=1.3592","x-requested-with":"XMLHttpRequest"},"referrer":"http://sip.ztm.kielce.pl/","referrerPolicy":"no-referrer-when-downgrade","body":null,"method":"GET","mode":"cors"})
.then(response => {
        return response.text();
    }).then(function(data) {
    console.log(data); // this will be a string
});

此脚本有效,但仅当我在 http://sip.ztm.kielce.pl 时有效。因此,我计划使用puppeteer从此站点上下文评估此脚本。

所以我写了Node.JS代码:

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    page.on('console', (log) => console[log._type](log._text));

    await page.goto('http://sip.ztm.kielce.pl');
    await page.waitFor(5000);

    await page.evaluate(() => {
        console.log('test');
        fetch("http://sip.ztm.kielce.pl/Home/CNR_GetVehicles?r=0W&d=&nb=", {"credentials":"include","headers":{"accept":"application/xml, text/xml, */*; q=0.01","accept-language":"pl;q=1.3592","x-requested-with":"XMLHttpRequest"},"referrer":"http://sip.ztm.kielce.pl/","referrerPolicy":"no-referrer-when-downgrade","body":null,"method":"GET","mode":"cors"})
        .then(response => {
                return response.text();
            }).then(function(data) {
            console.log(data); // this will be a string
        });
    });
    await browser.close();
})();

但是我得到的回应不是“正常”方式。 您有什么想法吗?

  • 由于API的安全性,此代码在第一个版本中也可能无效。我正在从Chrome开发者工具的“网络”标签中“正常”获取代码。

0 个答案:

没有答案