我在PythonAnywhere.com(PA)上部署了一个在我的本地计算机上运行的scrappy / flask项目,几乎完全适用于PA。我遇到的问题是,当我安排任务来运行我的蜘蛛时,没有启用管道。这显然是一个问题,因为我的MongoDB和图像管道不会更新。关于为什么我遇到这个问题以及如何解决它的任何想法?我可以按预期查询MongoDB和我的网站渲染,只是在爬网过程中没有启用管道,所以我留下了一个过时的网站。经过仔细检查,看起来爬虫使用的是不正确的僵尸程序。见下列输出;第一个来自我的本地机器,第二个来自PA:
正确 - 我的本地机器:
2019-03-16 15:51:12 [scrapy.utils.log] INFO: Scrapy 1.6.0 started (bot: mycorrectbot)
2019-03-16 15:51:12 [scrapy.utils.log] INFO: Versions: lxml 4.3.2.0, libxml2 2.9.9, cssselect 1.0.3, parsel 1.5.1, w3lib 1.20.0, Twisted 18.9.0, Python 3.7.1 (default, Nov 28 2018, 11:51:47) - [Clang 10.0.0 (clang-1000.11.45.5)], pyOpenSSL 19.0.0 (OpenSSL 1.1.1b 26 Feb 2019), cryptography 2.6.1, Platform Darwin-18.2.0-x86_64-i386-64bit
2019-03-16 15:51:12 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'mycorrectbot', 'DOWNLOAD_DELAY': 0.25, 'NEWSPIDER_MODULE': 'mine.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES': ['mine.spiders'], 'USER_AGENT': 'mine (+https://mine.com)'}
不正确 - 在PA上:
2019-03-16 22:57:11 [scrapy.utils.log] INFO: Scrapy 1.6.0 started (bot: scrapybot)
2019-03-16 22:57:11 [scrapy.utils.log] INFO: Versions: lxml 4.3.2.0, libxml2 2.9.9, cssselect 1.0.3, parsel 1.5.1, w3lib 1.20.0, Twisted 18.9.0, Python 3.7.0 (default, Aug 22 2018, 20:50:05) - [GCC 5.4.0 20160609], pyOpenSSL 19.0.0 (OpenSSL 1.1.1b 26 Feb 2019), cryptography 2.6.1, Platform Linux-4.4.0-1075-aws-x86_64-with-debian-stretch-sid
2019-03-16 22:57:11 [scrapy.crawler] INFO: Overridden settings: {}
项目结构:
├── LICENSE
├── README.md
├── flask_app
│ ├── __init__.py
│ ├── flask_app.py
│ ├── static
│ │ ├── css
│ │ │ └── home.css
│ │ ├── images
│ │ │ └── full
│ │ │ ├── 1.jpg
│ │ │ ├── 2.jpg
│ │ │ ├── 3.jpg
│ │ └── vendor
│ │ ├── bootstrap
│ │ │ ├── css
│ │ │ │ ├── bootstrap.css
│ │ │ │ ├── bootstrap.css.map
│ │ │ │ ├── bootstrap.min.css
│ │ │ │ └── bootstrap.min.css.map
│ │ │ └── js
│ │ │ ├── bootstrap.bundle.js
│ │ │ ├── bootstrap.bundle.js.map
│ │ │ ├── bootstrap.bundle.min.js
│ │ │ ├── bootstrap.bundle.min.js.map
│ │ │ ├── bootstrap.js
│ │ │ ├── bootstrap.js.map
│ │ │ ├── bootstrap.min.js
│ │ │ └── bootstrap.min.js.map
│ │ └── jquery
│ │ ├── jquery.js
│ │ ├── jquery.min.js
│ │ ├── jquery.min.map
│ │ ├── jquery.slim.js
│ │ ├── jquery.slim.min.js
│ │ └── jquery.slim.min.map
│ └── templates
│ └── index.html
├── scrapy_app
│ ├── scrapy_app
│ │ ├── __init__.py
│ │ ├── items.py
│ │ ├── middlewares.py
│ │ ├── pipelines.py
│ │ ├── settings.py
│ │ └── spiders
│ │ ├── __init__.py
│ │ └── mine.py
│ └── scrapy.cfg
└── requirements.txt
我的理解是,我无法直接在PA任务中为我的scrapy蜘蛛调用.py,而是必须依靠单独的文件来制作我通常会做的“scrapy crawl my spider”。但是,这似乎没有按预期工作。我引用了这个doc page但我必须遗漏一些东西。
这是我创建的用于进行备用呼叫并指向PA任务的文件:
import scrapy
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
from scrapy_app.spiders import mine
class MySpider('my spider'):
# Your spider definition
pass
if __name__ == "__main__":
process = CrawlerProcess(get_project_settings())
process.crawl(mine.MySpider)
process.start() # the script will block here until the crawling is finished
以下是我在PA上添加的计划任务(不在项目结构中 - 请忽略这一事实):
/home/myuserid/.virtualenvs/myvirtualenv/bin/python /home/myuserid/flask_app/flask_app/scheduler.py
提前感谢你的帮助!
哇。我做的比我需要的要复杂得多。我的PA和本地应用程序之间的唯一差异是我运行的虚拟环境的类型。我在PA上删除了推荐的VE,而是使用了venv。然后我编写了一个简单的脚本来激活VE,运行我的蜘蛛,然后停用。脚本如下(scheduler.sh):
#!/bin/bash
cd ~
source myvenv/bin/activate
cd /home/userid/project/scrapy_app/scrapy_app
scrapy crawl my spider
deactivate
尽管对PA和scrapy都有可怕的指导,但现在所有这些都按预期工作。希望这有助于其他人!