已掉线,重新登录

首页 > 绿虎论坛 > 历史版块 > 编程 > Python > 教程

标题: 基于白嫖 阿里云官方demo SCF给网页拍个照

作者: @Ta

时间: 2020-04-25发布,2020-04-25修改

点击: 6642

https://1911504709953557.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/puppeteer-test/html2png/?url=https://www.aliyun.com/product/fc

url=你想要的网址

这个是我从demo里找到的,阿里云官方应该是忘记关了,可以白嫖,不用付钱,
7419aeafc425f70909beb20dc98c629597001.png

[隐藏样式|查看源码]


『回复列表(7|隐藏机器人聊天)』

1.

分享源码

健健康康

(/@Ta/2020-04-25 21:25//)

2. @罐子,源码还要自己搭建,我这个是在官方文档中发现的demo,一时半会儿官方不会取消的。
并且,demo源码是python2.7

这里 我之前做过 基于php 的拍照 https://hu60.cn/q.php/bbs.topic.90400.html
(/@Ta/2020-04-25 21:30//)

3.

@胡椒舰长,demo源码是python2.7
发来可好。
健健康康

(/@Ta/2020-04-26 00:26//)

4.
const setup = require('./lib/setup');
const fs = require('fs');


module.exports.initializer = function(context, callback) {
  console.log('initializing');
  callback(null, ''); 
};

module.exports.handler = (req, resp, context) => {
  // Get the URL of the web page that needs screenshots from the request parameters
  let url = req.queries['url'];
  // If there is no url in the request parameters, set the default URL
  if (!url) {
    url = 'https://www.aliyun.com/product/fc';
  }
  // Whether it's HTTP or HTTPS, all prefixes are http
  if (!url.startsWith('https://') && !url.startsWith('http://')) {
    url = 'http://' + url;
  }
  
  screenshot(url, context)
    .then(outputFile => {
      // Get screenshot successful return
      resp.setStatusCode(200);
      resp.setHeader('content-type', 'image/png');
      resp.send(fs.readFileSync(outputFile))
    })
    .catch(err => {
      // Get screenshot failed return
      resp.setStatusCode(500);
      resp.setHeader('content-type', 'text/plain');

      resp.send(err.message);
    });
};

// Get screenshot method
async function screenshot(url, context) {
  // Open a new browser viewport
  let browser = await setup.getBrowser(context);

  const page = await browser.newPage();
  const outputFile = '/tmp/screenshot.png';

  let retry = 0;
  let success = false;
  
  // Try to read the screenshot less than 6 times
  do {
    try {
      console.log('goto', url);
      await page.goto(url);
      success = true;
    } catch(e) {
      console.log('error', e);
      retry++;

      if (retry >= 6) {
        throw e;
      }
    }
  } while(!success && retry < 6);

  // Set the path of the screenshot, whether it is full page or not, and type
  await page.screenshot({
    path: outputFile,
    fullPage: true,
    type: 'png'
  });

  return outputFile;
};




@罐子
(/@Ta/2020-04-26 09:54//)

5.

这个挺好能生成整个页面截图
手机

(/@Ta/2020-04-26 15:53//)

6. @童真再见,JS渲染还是有问题,我建议用  PHP的wkhtmltoImg,这个python版的 效果没wkhtmltoImg效果好,而且wkhtmltoImg可以直接生成pdf
(/@Ta/2020-04-26 16:06//)

7. 你要会python 这网页截图太简单
(/@Ta/2020-04-26 17:10//)

回复需要登录

8月23日 05:22 星期六

本站由hu60wap6驱动

备案号: 京ICP备18041936号-1