如何用PHP启动我的脚本呢?

2024-05-19 14:00:13 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的脚本,我在shell中输入“scrapy crawl quotes”来执行它,那么如何在php中启动这个命令?在

import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
    'http://quotes.toscrape.com/page/1/',
    'http://quotes.toscrape.com/page/2/',
]

def parse(self, response):
    for quote in response.css('div.quote'):
        yield {
            'text': quote.css('span.text::text').extract_first(),
            'author': quote.css('small.author::text').extract_first(),
            'tags': quote.css('div.tags a.tag::text').extract(),
        }

Tags: textdivcomhttpresponsetagspageextract
3条回答

要从PHP执行外部程序,您需要使用^{}。在

你的例子是:

<?php

exec('scrapy crawl quotes');

如果PHP是由Apache或Nginx执行的,请注意当前的工作目录。您很可能需要scrapy的完整路径,除非它已经在$PATH中。在

如果我理解正确的话,在命令行中运行php文件的正确方法是

php file.php

如果这不是你所要求的,请澄清。在

编辑

^{pr2}$

这将在php中运行terminal命令。在

Reference

我给你我的项目所用的东西

chdir('/Path_to_folder');
shell_exec('scrapy crawl yourSpider -a firstParam="'.$firstParam.'"' . ' -a secondParam="'.$secondParam.'"');

了解更多信息

Chdir

shell_exec

相关问题 更多 >