如何使用PHP启动我的scrapy脚本?
例如,这是我的脚本,我在shell中键入“scrapy crawl quotes”来执行它,那么如何在php中启动此命令呢如何使用PHP启动我的scrapy脚本?,php,python,scrapy,Php,Python,Scrapy,例如,这是我的脚本,我在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, respo
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(),
}
如果我理解正确,在命令行中运行php文件的正确方法是
php file.php
如果这不是你想要的,请澄清
编辑
<?php
$output = shell_exec('scrapy crawl quotes');
echo "<pre>$output</pre>";
?>
这将在php中运行terminal命令
从要使用的PHP执行外部程序 你的例子是:
chdir('/Path_to_folder');
shell_exec('scrapy crawl yourSpider -a firstParam="'.$firstParam.'"' . ' -a secondParam="'.$secondParam.'"');
我给你我的项目用的东西
更多信息
谢谢您的回答,那么我如何定义python脚本的路径呢?您可以在shell中执行python/path/to/file/file.py_exec@ZouhirSarrar如果python脚本与phone脚本位于同一位置,则无需执行文件路径。它还将以www数据用户的身份运行命令。@ZouhirSarrar我的答案还将以代码格式将结果打印到屏幕上,如您所见。如果这有帮助的话,我将非常感谢投票或支票。谢谢你的回答,我尝试了你的脚本,但是我如何用php打印爬网的输出?你想打印项目吗?在项目的主目录中有执行“scrapy crawl myspider”的条件吗?是的,表格中的项目例如,对于第一个问题,请阅读文档:),对于第二个问题,如果您没有义务打印项目,最好将结果导出为csv或json
chdir('/Path_to_folder');
shell_exec('scrapy crawl yourSpider -a firstParam="'.$firstParam.'"' . ' -a secondParam="'.$secondParam.'"');