我正在做刮擦,刮一个网站,并获取所有的信息。
实际上,我有3只具有不同数据的蜘蛛,我用以下结构在同一个文件夹中创建了这3只蜘蛛
scrapy.cfg
myproject/
__init__.py
items.py
pipelines.py
settings.py
spiders/
__init__.py
spider1.py
spider2.py
spider3.py现在,当我们运行特定的蜘蛛时,我需要通过管道创建一个csv文件,例如,使用这个蜘蛛名。
spider1.csv,spider2.csv,spider3.csv and so on (蜘蛛不受限制,它们可能更多)>>根据蜘蛛的数量和蜘蛛的名称,我想创建csv文件
在这里,我们是否可以在pipeline.py中创建多个管道?此外,如果存在多个蜘蛛,如何动态创建具有蜘蛛名称的csv文件。
这里我有3个蜘蛛,我想一次运行所有3个蜘蛛(使用scrapyd),当我运行所有3个蜘蛛时,应该创建3个csv文件及其蜘蛛名。我想安排这些蜘蛛每6个小时跑一次。如果我的解释有问题,请纠正我,让我知道如何做到这一点。
提前感谢
编辑代码:以为例,我只为spider1.py粘贴代码
spider1.py中的代码
class firstspider(BaseSpider):
name = "spider1"
domain_name = "www.example.com"
start_urls = [
"www.example.com/headers/page-value"
]
def parse(self, response):
hxs = HtmlXPathSelector(response)
........
.......
item = Spider1Item()
item['field1'] = some_result
item['field2'] = some_result
.....
.....
return itemPipeline.py代码:
import csv
from csv import DictWriter
class firstspider_pipeline(object):
def __init__(self):
self.brandCategoryCsv = csv.writer(open('../%s.csv' % (spider.name), 'wb'),
delimiter=',', quoting=csv.QUOTE_MINIMAL)
self.brandCategoryCsv.writerow(['field1', 'field2','field3','field4'])
def process_item(self, item, spider):
self.brandCategoryCsv.writerow([item['field1'],
item['field2'],
item['field3'],
item['field4'])
return item 如前所述,当我使用蜘蛛名运行上面的蜘蛛时,将动态创建一个带有蜘蛛名的csv文件……但是现在,当我运行其余的蜘蛛(如spider2,spider3,spider3 )时,应该生成具有相应蜘蛛名的csv文件。
我想在保存到数据库时实现同样的功能,我的意思是当我运行spider1时,spider1的所有数据都应该保存到一个具有相对蜘蛛名的表中。在这里,对于每个蜘蛛,我有不同的sql查询(因此需要编写不同的管道类)
对不起,如果哪里错了,我希望它能很好地解释,如果没有,请告诉我。
发布于 2012-07-06 09:40:56
你一般都在正确的轨道上。
但我可以立即指出以下几点:
__init__-method,那么您几乎肯定不需要一个类,但是函数会做得很好。少杂乱=更好的代码!1. You don't need a BaseSpider class as far as i can see. What would be the difference between the base-class and the sub-classes? Deriving classes doesn't make your program OO, or better, or whatever. Search for [Liskovs principle](https://stackoverflow.com/questions/56860/what-is-the-liskov-substitution-principle) to get a general understanding for when a subclass may be appropriate in python. (It's somewhat a reverse logic, but it's one of the fastest ways to see if you should subclass or change your approach.)
2. There is a distinct difference between python class-variables which are declared immediatly after the class declaration and instance-variables which are initialized in the `__init__` method. Class variables are **SHARED** between all instances of a class, where instance-variables are private to the individual instances. You almost never want to use class-variables, which are a _Singleton-Pattern_, something you want to avoid in most cases, because it causes headaches and grievance in debugging.
因此,我将修改您的Spider-class,如:
class Spider(object):
def __init__(self, name, url=None):
self.name = name
self.domain_name = url
self.start_urls = [url]
...
crawlers = [Spider('spider %s' %i) for i in xrange(4)] #creates a list of 4 spiders 但是,也许您正在使用声明式元类方法,但我从您发布的代码中看不到这一点。
如果你想并行运行你的爬虫,你应该考虑threading-module。它用于与multiprocessing-module相反的连续I/O操作,后者用于并行计算。
你在概念上是正确的。将项目分解成小块,每次遇到错误时都会回来。
只是不要指望在这样的问题上得到一个完整的答案:“我不想重新创建谷歌,我如何才能以最好的方式和最短的时间做到这一点!”
https://stackoverflow.com/questions/11343973
复制相似问题