在AWS Lambda函数中使用scrapy作为图层

时间:2019-03-06 11:47:47

标签: python amazon-web-services web-scraping scrapy aws-lambda

我正在尝试在AWS Lambda函数中使用scrapy作为图层。

我用pip在目录中安装了scrapy:

<class 'queens.Queens'>
<class 'list'>

目录格式与我已经在使用的所有层一样。 我压缩并上传到图层。 我将该层包括到了lambda函数中。 我导入了scrapy:

import array
import copy
class Queens:
    __slots__ = ["n", "qlist"]

    def __init__(self, n, qa=None):
        self.n = n # store for print function
        if qa == None:
            self.qlist = array.array("h")
        elif type(qa) == list:
            self.qlist = array.array("h", qa)
        else:
            assert type(qa) == array.array
            self.qlist = qa # list of positions for each line

    def __str__(self):
        out = ""
        for q in range(self.n):
            if q == 0:
                out += "|"
            else:
                out += "\n|"
            for space in range(self.n):
                if q < len(self.qlist) and space == self.qlist[q]:
                    out += "Q|"
                else:
                    out += " |"
        return out

    def branch(self):
        out = []
        for x in range(self.n):
            if x not in self.qlist:
                qlist = copy.deepcopy(self.qlist)
                qlist.append(x)
                out.append(Queens(self.n, qlist))
        return out

    def validate(self):
        for y in range(len(self.qlist)):
            # don't need to check horizontal; 
            # data structure doesn't let you place them
            # don't need to check vertical;
            # branching eliminates those
            # check diagonals
            for y2 in range(len(self.qlist)):
                if y != y2:
                    expected = self.qlist[y] - y + y2
                    if 0 <= expected < self.n and self.qlist[y2] == expected:
                        return -1
                    expected = self.qlist[y] + y - y2
                    if 0 <= expected < self.n and self.qlist[y2] == expected:
                        return -1
        return self.n - len(self.qlist)

if __name__ == "__main__":
    q = Queens(4)
    print(q.validate())
    q = Queens(4, [0, 1, 2])
    print(q.validate())

当我运行项目时,出现此错误:

pip install scrapy 

import scrapy

1 个答案:

答案 0 :(得分:0)

正如@balderman的评论所建议的那样,您需要本机库才能使Scrapy运行。 这是非常可行的,我将尝试尽可能简单地解释。

用于scrapy的二进制文件必须在与lambda实例相同的环境中进行编译。 Lambda使用AWS Linux启动。

您可以启动运行AmazonLinux的EC2或使用docker,最简单的方法是启动docker容器。

import { AppRegistry, Dimensions } from 'react-native';
import DrawerNavigator from 'react-navigation';
// import App from './App';
//Components
import HomeComponent from './components/HomeComponent';
import InfoComponent from './components/InfoComponent';
import SettingsComponent from './components/SettingsComponent';
import CloudComponent from './components/CloudComponent';
//Screen names
import { Home, Info, Settings, Cloud } from './screenNames';
//Screen size
var {height, width} = Dimensions.get('window');

let routeConfigs = {
    Home: {
        path: '/',
        screen: HomeComponent,
    },
    Info: {
        path: '/info',
        screen: InfoComponent,
    },
    Settings: {
        screen: SettingsComponent,
    },
    Cloud: {
        screen: CloudComponent,
    },
};
let drawerNavigatorConfig = {    
    initialRouteName: Home,
    drawerWidth: width / 2,
    drawerPosition: 'left',
    drawerOpenRoute: 'DrawerOpen',
    drawerCloseRoute: 'DrawerClose',
    drawerToggleRoute: 'DrawerToggle',  
    // drawerBackgroundColor: 'orange',
    contentOptions: {
        activeTintColor: 'red',
    },
    order: [Info, Settings, Cloud, Home]
};
const App = DrawerNavigator(routeConfigs, drawerNavigatorConfig);
AppRegistry.registerComponent('tutorialProject', () => App);

现在,您需要将所有.so文件下载/解压缩到目录中,然后将其压缩。另外,请确保将所有.so文件保留在zip中名为lib的文件夹中。压缩后,压缩后的外观应类似于以下内容:

$ sudo docker run -it amazonlinux bash

然后,您可以将其压缩并上传为图层。它将被上传到您的Lambda容器中的/ opt /中。 AWS在many other locations中的/ opt / lib下查找库文件。

您面临的挑战性部分是弄清楚如何获取所有必需的.so文件,以使scrapy正常运行。