代码之家  ›  专栏  ›  技术社区  ›  weaveoftheride

使用节点截图URL-异步

  •  0
  • weaveoftheride  · 技术社区  · 8 年前

    这是我的密码:

    const fs = require('fs');
    const screenshot = require('screenshot-stream');
    const urlp = require('url');
    var urls=[
    'https://archive.org/details/8bitrecs',
    'http://hackaday.com/',
    'http://techcrunch.com/2012/02/16/auraslate-is-an-open-source-android-tablet-for-hackers/',
    'http://www.english.illinois.edu/-people-/faculty/debaron/482/482readings/greenfield.html',
    'http://sustain.rca.ac.uk/Sustain-Talks'];
    urls.forEach(function(url){
        const stream = screenshot(url, '1024x768', {crop: true});
        stream.pipe(fs.createWriteStream(urlp.parse(url).hostname + 'test-1024x768.png')); 
    });
    

    我该怎么做?

    我想截图工作,但捕捉错误,而不是阻止如果一个网址是不可访问的

    更新: https://www.npmjs.com/package/screenshot-promise 工作得更好,虽然下面的代码仍然减慢了我的电脑很多!

    const screenshotPromise = require('screenshot-promise');
    

    ...

    urls.forEach(function(url) {
    
    
    
    const promise = screenshotPromise(url, '1024x768', {crop: true}).then(buf => {
        fs.writeFileSync(urlp.parse(url).hostname + 'test-1024x768.png', buf);
    });
    
    
    promise.then((value) => {
      // value is whatever we passed in the resolve(...) function above.
      // It doesn't have to be a string, but if it is only a succeed message, it probably will be.
      console.log(value);
    });
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Seblor Oybek    8 年前

    您未能在此处添加的是错误:

    events.js:160

    错误:无法加载url: http://techcrunch.com/2012/02/16/auraslate-is-an-open-source-android-tablet-for-hackers/

    (e:---\node\u modules\screenshot流\索引js:77:16)
    一点钟(events.js:96:13)

    问题是模块 screenshot-stream 正在使用PhantomJS,而PhantomJS无法访问输出错误的页面。

    此错误似乎与此问题有关: https://github.com/ariya/phantomjs/issues/10460 .

    Techcrunch.com 以及Aol.com 似乎使用网络字体(例如“BebasNeue-webfont.ttf),Qt将其加载为应用程序字体。那里可能出了点问题。

    我的建议是使用谷歌的Puppeter,包括一个内置的截图方法: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagescreenshotoptions

        2
  •  0
  •   weaveoftheride    8 年前

       const puppeteer = require('puppeteer');
    const urlp = require('url');
    var URL = require('url-parse');
    var urls = [
        'https://archive.org/details/8bitrecs',
        'http://hackaday.com/',
        'http://techcrunch.com/2012/02/16/auraslate-is-an-open-source-android-tablet-for-hackers/',
        'http://www.english.illinois.edu/-people-/faculty/debaron/482/482readings/greenfield.html',
        'http://sustain.rca.ac.uk/Sustain-Talks',
        'https://www.quintessentially.com/',
        'https://www.producthunt.com/tech/ux-project-checklist',
        'https://freedom.press/',
        'http://issuu.com/search?q=vintage+motorcycle',
        'http://www.pocketmod.com/v2/',
        'https://www.metamind.io/',
        'http://nautil.us/blog/chernobyls-hot-mess-the-elephants-foot-is-still-lethal',
        'https://www.instructables.com/id/Tool-Storage-Hacks-or-How-to-Hang-Those-Black-Frid/',
        'https://www.zippi.co.uk/framed-photo-print'
    ];
    var getLocation = function(href) {
        var l = document.createElement("a");
        l.href = href;
        return l;
    };
    (async() => {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        page.waitForNavigation({
            timeout: 40000
        });
        for (let i = 0; i < urls.length; i++) {
            const url = urls[i];
            var url1 = new URL(url);
            try {
                await page.goto(`${url}`);
                await page.screenshot({
                    path: 'images/' + url1.hostname + '.png'
                });
            } catch (error) {
                console.log(error.message);
                // await page.close();
                // await browser.close();
                // process.exit(1);
                continue;
            }
        }
    })();
    
    推荐文章