实际上你需要一个编辑器来编写你的蜘蛛程序来抓取和抓取网页,在你开始真正编写蜘蛛程序之前,shell 更像是一个测试环境。所以从终端
$workon my_virtual
在特定的虚拟环境中工作。 cd 到相应的目录
$pip install scrapy
在你的虚拟环境中安装scrapy
$scrapy startproject myspider
这会在您所在的目录中启动并运行您需要的一切。
$scrapy genspider <spidername> <base_url>
这将自动为您生成一个蜘蛛。有关如何操作的更多详细信息,您可能需要参考scrapy 文档。
您可以使用 subprocess 模块与另一个 python 程序一起执行 python 脚本。
def otherfunc(crawler_output):
#do something with the gotten out put
def myfunction():
os.chdir('scrapy directory')
subprocess.Popen(['scrapy', crawl], shell=True)
output, err = Popen.communicate()
os.chdir('initail working directory')
otherfunction(output)
此方法直接从 shell 上的 scrapy 爬虫获取输出并将其提供给您的代码。另一种使用类似原理更安全的方法是
def otherfunc(crawler_output):
#locate the file within your directories
#do something with the gotten out put
def myfunction():
os.chdir('scrapy directory')
subprocess.Popen(['scrapy', crawl, name, -o, file.csv], shell=True)
otherfunction(file.csv)
这样你将处理一个文件,但文件的内容将是从scrapy得到的结果