python 如何在Amazon Lambda函数中运行报纸

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 如何在Amazon Lambda函数中运行报纸相关的知识,希望对你有一定的参考价值。

How to run [Newspaper](https://github.com/codelucas/newspaper) (the Python 2.7 version) in an Amazon Lambda function:

- Start a new EC2 instance with the Amazon Linux AMI
- `sudo yum install gcc gcc-c++ libjpeg-devel zlib-devel libevent-devel libxml2-devel libxslt-devel libpng-devel`
- `sudo yum install python27-devel python27-pip`
- `virtualenv env`
- `source env/bin/activate`
- `sudo /usr/bin/easy_install lxml`
- `pip install newspaper`
- `nano env/local/lib/python2.7/site-packages/newspaper/settings.py`
  - change `DATA_DIRECTORY` variable value to `'/tmp/.newspaper_scraper'`
- `zip -9 bundle.zip lambda_function.py`
- `cd $VIRTUAL_ENV/lib/python2.7/site-packages`
- `zip -r9 ~/bundle.zip *`
- `cd $VIRTUAL_ENV/lib64/python2.7/site-packages`
- `zip -r9 ~/bundle.zip *`
- Upload the `bundle.zip` file to your Lambda function
  - This assumes a default Handler set to `lambda_function.lambda_handler`
- Delete your EC2 instance
from newspaper import Article

def lambda_handler(event, context):
    url = event['url']
    article = Article(url)
    article.download()
    article.parse()

    return {
        'content' : article.text
    }

以上是关于python 如何在Amazon Lambda函数中运行报纸的主要内容,如果未能解决你的问题,请参考以下文章

从节点应用程序调用 amazon lambda 函数

Amazon Echo Lambda 函数。控制台打印在哪里?

如何添加 CloudFront 作为 lambda 函数的触发器?

Amazon S3 在另一个账户中触发另一个 Lambda 函数

使用 lambda 函数 nodejs 从 Amazon Redshift 查询选择

访问 AWS Lambda 的 Amazon CloudWatch 日志