结构是这样的
maple/
├── __init__.py
├── items.py
├── models.py
├── pipelines.py
├── settings.py
└── spiders
├── news.py
└── __init__.py
news.py有
class Spider(scrapy.spiders.Spider):
name = 'news'
和
class BsSpider(scrapy.spiders.Spider):
name = 'bsnews'
models.py(使用sqlalchemy)有News(base)
和BsNews(base)
两个表
items.py有NewsItem(scrapy.Item)
和BsNewsItem(scrapy.Item)
请教pipelines.py应该怎么写?
只有一个Spider时,scrapy crawl news
def process_item(self, item, spider):
exsit_url = self.session.query(News.url).\
filter_by(url=item['url']).first()
if not exsit_url:
news = News()
news.title = item['title']
news.url = item['url']
news.time = item['time']
news.content = item['content']
self.session.add(news)
self.session.commit()
能够正常运行
现在我要运行scrapy crawl bsnews如何指定存储的表为BsNews?
第一次接触scrapy,求大神解答
具体源码
你可以在 pipeline 里判断是哪个爬虫的结果:
对于这种多个爬虫在一个工程里的,需要不同爬虫在 pipeline 里使用不同逻辑的问题 scrapy 的作者是这么解释的。
去看看