我正在从 php 运行一个 python 脚本,在那里我做了一些报废。该脚本在 Ubuntu 终端上执行时运行正常,但在 Apache 日志上给出了这些行:
df=BUscrap(sys.argv[1])
File "/var/www/scrapbot.chambeala.com/./realScrapper.py", line 261, in BUscrap
driver=webdriver.Firefox(service=service, options=options)
File "/var/www/myenv/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 68, in __init__
super().__init__(command_executor=executor, options=self.options)
File "/var/www/myenv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 206, in __init__
self.start_session(capabilities)
File "/var/www/myenv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 291, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
File "/var/www/myenv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
self.error_handler.check_response(response)
File "/var/www/myenv/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Failed to read marionette port
这是 Python 中我设置并启动 Geckodriver 的部分:
def BUscrap(limit=""):
limit=int(limit)
print("Capturando nuevos trabajos...")
options=webdriver.FirefoxOptions()
service = Service(executable_path='/usr/local/bin/geckodriver')
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument(f'--proxy-server={proxy_server_url}')
options.add_argument('user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36')
driver=webdriver.Firefox(service=service, options=options)
driver.implicitly_wait(10)
这是来自 PHP 的调用:
<?php
$cmd = escapeshellcmd("./realScrapper.py " .$_POST["ligas"]);
if(isset($_POST['ejecutar'])) {
echo '<div class="output">';
while (@ ob_end_flush());
$proc = popen($cmd, 'r');
echo '<pre>';
while (!feof($proc))
{
echo fread($proc, 4096);
@ flush();
}
echo '</pre>';
$_POST = array();
echo '</div>';
}
?>
Firefox 和 Geckodriver 具有执行权限,PHP 代码与其他脚本可以正常运行。 Firefox 不是通过 snap 安装的,而是通过 debian 包安装的。
Python版本是3.10.6,Selenium版本是4.10.0, 火狐版本是107.0.1, Geckodriver 版本是 0.33.0
如果需要更多信息,请告诉我。
我试图从 PHP 执行 Python 抓取脚本,但网页闲置然后停止。