我在 aws lambda 上运行/部署了一个基本的 selenium 脚本。
Chrome 和 chromedriver 并通过无服务器安装为层(在 /opt 上可用)。
脚本有效...但只是在某些时候而且很少大规模(异步调用超过 5 个实例)。
我在一个简单的 for 循环中调用该函数(最多约 200 次迭代)
response = client.invoke(
FunctionName='arn:aws:lambda:us-east-1:12345667:function:selenium-lambda-dev-hello',
InvocationType='Event', #|'RequestResponse'|'Event' (async)| DryRun'
LogType='Tail',
#ClientContext='string',
Payload=event_payload,
#Qualifier='24'
)
其他运行,该行启动selenium驱动时进程挂起
driver = webdriver.Chrome('/opt/chromedriver_89', chrome_options=options)
驱动程序的其他迭代失败/抛出“等待渲染器异常超时” 我认为这通常是由于 chromedriver/chrome 不匹配造成的。我已经检查并验证了我的版本是否匹配和兼容(就像我说的,它们有时确实有效)。
我想我正在寻找一些想法/方向来解决这个问题。我的印象是 lambda 函数的每次调用都在一个单独的环境中,那么为什么增加调用量会对我的脚本运行产生不利影响?
任何想法或想法都将不胜感激!
答案 0 :(得分:0)
在评论中讨论。
没有完整的解决方案,但有助于改善这种情况的是增加 Lambda 服务的内存。
尝试/考虑的替代方案:
requests
和 lxml
通过网络级别查询页面并消除对 Chrome 的需求。我最近做了类似的事情来支持another stack question。你可以看到它很相似,但并不完全相同。
转到一个 url 并从 xpath 获取一些文本:
from lxml import html
import requests
import json
url = "https://nonfungible.com/market/history"
response = requests.get(url)
page = html.fromstring(response.content)
datastring = page.xpath('//script[@id="__NEXT_DATA__"]/text()')
HtmlUnitDriver
或 phantomjs
。这两者都比 chrome 轻得多,并且不需要安装浏览器(您可以直接从库中运行它)您只需要更改驱动程序初始化,脚本的其余部分(理论上)应该可以工作。
PhantomJS:
$ pip install selenium
$ brew install phantomjs
from selenium import webdriver
driver = webdriver.PhantomJS()
单元驱动程序:
from selenium import webdriver
driver = webdriver.Remote(
desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT)