将Scrapy项目打包成exe
用到的工具:pyinstaller
步骤:
1、在项目目录下创建脚本start_spider.py文件(注意:名字任意,和scrapy.cfg文件同级)
2、start_spider.py文件内容:
# -*- coding: utf-8 -*- from scrapy.crawler import CrawlerProcess from scrapy.utils.project import get_project_settings # 如果报错可以注释掉 # import robotparser import scrapy.spiderloader import scrapy.statscollectors import scrapy.logformatter import scrapy.dupefilters import scrapy.squeues import scrapy.extensions.spiderstate import scrapy.extensions.corestats import scrapy.extensions.telnet import scrapy.extensions.logstats import scrapy.extensions.memusage import scrapy.extensions.memdebug import scrapy.extensions.feedexport import scrapy.extensions.closespider import scrapy.extensions.debug import scrapy.extensions.httpcache import scrapy.extensions.statsmailer import scrapy.extensions.throttle import scrapy.core.scheduler import scrapy.core.engine import scrapy.core.scraper import scrapy.core.spidermw import scrapy.core.downloader import scrapy.downloadermiddlewares.stats import scrapy.downloadermiddlewares.httpcache import scrapy.downloadermiddlewares.cookies import scrapy.downloadermiddlewares.useragent import scrapy.downloadermiddlewares.httpproxy import scrapy.downloadermiddlewares.ajaxcrawl # import scrapy.downloadermiddlewares.chunked 如果报错可以注释掉 import scrapy.downloadermiddlewares.decompression import scrapy.downloadermiddlewares.defaultheaders import scrapy.downloadermiddlewares.downloadtimeout import scrapy.downloadermiddlewares.httpauth import scrapy.downloadermiddlewares.httpcompression import scrapy.downloadermiddlewares.redirect import scrapy.downloadermiddlewares.retry import scrapy.downloadermiddlewares.robotstxt import scrapy.spidermiddlewares.depth import scrapy.spidermiddlewares.httperror import scrapy.spidermiddlewares.offsite import scrapy.spidermiddlewares.referer import scrapy.spidermiddlewares.urllength import scrapy.pipelines import scrapy.core.downloader.handlers.http import scrapy.core.downloader.contextfactory # 在项目中自己导入的类库(这个是我的项目用到的) import scrapy.pipelines.images from scrapy.pipelines.images import ImagesPipeline from scrapy.pipelines.media import MediaPipeline from sqlalchemy import Column, Integer, String, DateTime, create_engine from sqlalchemy.orm import sessionmaker import os, re import scrapy import queue import threading import requests process = CrawlerProcess(get_project_settings()) process.crawl('自己项目中爬虫的名字') process.start()
3、cd到项目目录,start_spider.py文件所在目录,执行pyinstaller strat_spider.py命令,等待完成,会生成build和dist文件夹,保留dist文件即可。
4、在项目目录下,进入dist文件夹,找到start_spider.exe点击执行(建议将该文件拖到控制台执行,方便查看具体报错,不然直接执行有错误一闪就没了)
注意事项及常见报错:
1、执行中缺少什么模块就导入什么,实在找不到的注释试试,从新打包
2、出现KeyError: 'Spider not found:爬虫名,可以将项目源码和打包程序放在一块,即打包时生成时的目录结构,不要改变,拷贝时连同项目整体拷贝,亲测有效。