如何从 AWS Lambda 运行 Scrapy 蜘蛛?

12 amazon-web-services scrapy python-3.x aws-lambda

我正在尝试从 AWS Lambda 中运行一个爬虫蜘蛛。这是我当前脚本的样子,它正在抓取测试数据。

import boto3
import scrapy
from scrapy.crawler import CrawlerProcess

s3 = boto3.client('s3')
BUCKET = 'sample-bucket'

class BookSpider(scrapy.Spider):
    name = 'bookspider'
    start_urls = [
        'http://books.toscrape.com/'
    ]

    def parse(self, response):
        for link in response.xpath('//article[@class="product_pod"]/div/a/@href').extract():
            yield response.follow(link, callback=self.parse_detail)
        next_page = response.xpath('//li[@class="next"]/a/@href').extract_first()
        if next_page:
            yield response.follow(next_page, callback=self.parse)

    def parse_detail(self, response):
        title = response.xpath('//div[contains(@class, "product_main")]/h1/text()').extract_first()
        price = response.xpath('//div[contains(@class, "product_main")]/'
                               'p[@class="price_color"]/text()').extract_first()
        availability = response.xpath('//div[contains(@class, "product_main")]/'
                                      'p[contains(@class, "availability")]/text()').extract()
        availability = ''.join(availability).strip()
        upc = response.xpath('//th[contains(text(), "UPC")]/'
                             'following-sibling::td/text()').extract_first()
        yield {
            'title': title,
            'price': price,
            'availability': availability,
            'upc': upc
        }

def main(event, context):
    process = CrawlerProcess({
        'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
        'FEED_FORMAT': 'json',
        'FEED_URI': 'result.json'
    })

    process.crawl(BookSpider)
    process.start() # the script will block here until the crawling is finished

    data = open('result.json', 'rb')
    s3.put_object(Bucket = BUCKET, Key='result.json', Body=data)
    print('All done.')

if __name__ == "__main__":
    main('', '')
Run Code Online (Sandbox Code Playgroud)

我首先在本地测试了这个脚本,它正常运行,抓取数据并将其保存到“results.json”,然后将其上传到我的 S3 存储桶。

然后,我按照此处的指南配置了我的 AWS Lambda 函数:https : //serverless.com/blog/serverless-python-packaging/ 并成功导入 AWS Lambda 中的 Scrapy 库以供执行。

但是,当该脚本在 AWS Lambda 上运行时,它不会抓取数据,只会为results.json 不存在抛出错误

任何配置运行 Scrapy 或有解决方法或可以为我指出正确方向的人都将受到高度赞赏。

谢谢。

Joe*_*Joe 6

只是在寻找其他东西时遇到了这个,但在我的头顶上..

Lambdas 在 /tmp 中提供临时存储,所以我建议设置

'FEED_URI': '/tmp/result.json'
Run Code Online (Sandbox Code Playgroud)

进而

data = open('/tmp/result.json', 'rb')
Run Code Online (Sandbox Code Playgroud)

在 lambdas 中使用临时存储可能有各种各样的最佳实践,所以我建议花一些时间阅读这些。