- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在从脚本运行 scrapy,但它所做的只是激活蜘蛛。它不会通过我的项目管道。我读过 http://scrapy.readthedocs.org/en/latest/topics/practices.html但它没有说明包含管道。
我的设置:
Scraper/
scrapy.cfg
ScrapyScript.py
Scraper/
__init__.py
items.py
pipelines.py
settings.py
spiders/
__init__.py
my_spider.py
我的脚本:
from twisted.internet import reactor
from scrapy.crawler import Crawler
from scrapy.settings import Settings
from scrapy import log, signals
from Scraper.spiders.my_spider import MySpiderSpider
spider = MySpiderSpider(domain='myDomain.com')
settings = get_project_settings
crawler = Crawler(Settings())
crawler.signals.connect(reactor.stop, signal=signals.spider_closed)
crawler.configure()
crawler.crawl(spider)
crawler.start()
log.start()
log.msg('Reactor activated...')
reactor.run()
log.msg('Reactor stopped.')
我的管道:
from scrapy.exceptions import DropItem
from scrapy import log
import sqlite3
class ImageCheckPipeline(object):
def process_item(self, item, spider):
if item['image']:
log.msg("Item added successfully.")
return item
else:
del item
raise DropItem("Non-image thumbnail found: ")
class StoreImage(object):
def __init__(self):
self.db = sqlite3.connect('images')
self.cursor = self.db.cursor()
try:
self.cursor.execute('''
CREATE TABLE IMAGES(IMAGE BLOB, TITLE TEXT, URL TEXT)
''')
self.db.commit()
except sqlite3.OperationalError:
self.cursor.execute('''
DELETE FROM IMAGES
''')
self.db.commit()
def process_item(self, item, spider):
title = item['title'][0]
image = item['image'][0]
url = item['url'][0]
self.cursor.execute('''
INSERT INTO IMAGES VALUES (?, ?, ?)
''', (image, title, url))
self.db.commit()
脚本输出:
[name@localhost Scraper]$ python ScrapyScript.py
2014-08-06 17:55:22-0400 [scrapy] INFO: Reactor activated...
2014-08-06 17:55:22-0400 [my_spider] INFO: Closing spider (finished)
2014-08-06 17:55:22-0400 [my_spider] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 213,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 18852,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2014, 8, 6, 21, 55, 22, 518492),
'item_scraped_count': 51,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2014, 8, 6, 21, 55, 22, 363898)}
2014-08-06 17:55:22-0400 [my_spider] INFO: Spider closed (finished)
2014-08-06 17:55:22-0400 [scrapy] INFO: Reactor stopped.
[name@localhost Scraper]$
最佳答案
@Pawel's 和 the docs'解决方案对我不起作用,在查看 Scrapy's source code 之后,我意识到在某些情况下它没有正确识别设置模块。我想知道为什么管道没有被使用,直到我意识到它们从来没有从脚本中找到。
正如文档和 Pawel 所说,我使用的是:
from scrapy.utils.project import get_project_settings
settings = get_project_settings()
crawler = Crawler(settings)
但是,调用时:
print "these are the pipelines:"
print crawler.settings.__dict__['attributes']['ITEM_PIPELINES']
我得到了:
these are the pipelines:
<SettingsAttribute value={} priority=0>
settings
未正确填充。
我意识到需要的是项目设置模块的路径,相对于包含调用 Scrapy 的脚本的模块,例如scrapy.myproject.settings
。然后,我创建了 Settings()
对象,如下所示:
from scrapy.settings import Settings
settings = Settings()
os.environ['SCRAPY_SETTINGS_MODULE'] = 'scraper.edx_bot.settings'
settings_module_path = os.environ['SCRAPY_SETTINGS_MODULE']
settings.setmodule(settings_module_path, priority='project')
我使用的有效导入管道的完整代码是:
from twisted.internet import reactor
from scrapy.crawler import Crawler
from scrapy import log, signals
from scrapy.settings import Settings
from scrapy.utils.project import get_project_settings
from scrapy.myproject.spiders.first_spider import FirstSpider
spider = FirstSpider()
settings = Settings()
os.environ['SCRAPY_SETTINGS_MODULE'] = 'scrapy.myproject.settings'
settings_module_path = os.environ['SCRAPY_SETTINGS_MODULE']
settings.setmodule(settings_module_path, priority='project')
crawler = Crawler(settings)
crawler.signals.connect(reactor.stop, signal=signals.spider_closed)
crawler.configure()
crawler.crawl(spider)
crawler.start()
log.start(loglevel=log.INFO)
reactor.run()
关于python - 从不包括管道的脚本运行 scrapy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25170682/
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 6 年前。 Improve t
notificationCenterPublisher = NotificationCenter.default .publisher(for: .NSManagedObjec
我有以下 Rust 代码: #[inline(never)] fn x() -> i32 { 21 } pub fn main() -> i32 { x() } 如果没有优化 (-C
notificationCenterPublisher = NotificationCenter.default .publisher(for: .NSManagedObjec
我有以下 Rust 代码: #[inline(never)] fn x() -> i32 { 21 } pub fn main() -> i32 { x() } 如果没有优化 (-C
假设我的 ASPX 页面没有内联 C# 代码块。 所以,我可以安全地设置 ...在我的 web.config 文件中,不用担心编译错误。 就性能而言,使用以下设置是否会有任何损失? 即“自动”检测
应用程序.js var win1 = Titanium.UI.createWindow({ title:'Tab 1', backgroundColor: 'black', l
基本上,我正在为实现多级优先级队列的 xv6 内核实现一个调度程序。我有一个严重的问题,我不明白,我类(class)的助教不明白,我已经错过了这个项目的最后期限,所以现在帮助我不会给我任何加分 - 但
我想避免 git 自动 merge 我的 composer.json。这样我就可以在 develop 分支中有一个使用 dev-develop 包的 composer.json,而 master 中的
当比较两种不同的算法实现时(因此,不关心它们的绝对性能,而只关心相对性能)我是否最好强制 Java 只运行解释代码? 也就是说,打开 -Xint 标志会更好吗? 最佳答案 我不认为禁用 JIT 会更好
class A{ const size_t number; public: A(size_t number): number(number) {} void f(){
问题 寻找在以下之间切换的方法: 总是 从不 仅在静默模式下 仅当不处于静默模式时 这些选项在手机上的路径---菜单>>设置>>声音>>振动---找到。 通过手机导航很容易更改(顺便说一句,我的手机是
如何设置电源设置关闭:从不(关闭显示器=从不,让计算机进入休眠状态=从不),通过c#代码 最佳答案 这个问题中给出的链接可以告诉你一个方法。 Programmatically change Windo
我是一名优秀的程序员,十分优秀!