scrapy简明教程
2015-09-06 22:26
357 查看
scrapy 0.24 简明教程
新建工程
scrapy startproject <project-name>
目录结构如下:
│ scrapy.cfg └─demo │ items.py │ pipelines.py │ settings.py │ __init__.py │ └─spiders __init__.py
添加item
# -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentation in: # http://doc.scrapy.org/en/latest/topics/items.html import scrapy from scrapy.item import Item, Field class DemoItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() pass class DmozItem(Item): title = Field() link = Field() desc = Field()
添加爬虫
from scrapy.spider import BaseSpider from demo.items import DmozItem class DmozSpider(BaseSpider): name = "dmoz" allowed_domains = ["dmoz.org"] start_urls = [ "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/", "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/" ] def parse(self, response): for sel in response.xpath('//ul/li'): item = DmozItem() item['title'] = sel.xpath('a/text()').extract() item['link'] = sel.xpath('a/@href').extract() item['desc'] = sel.xpath('text()').extract() yield item
相关文章推荐
- poi操作excel文件
- git cherry-check error "fatal: bad object"
- maven项目管理之-10-创建web项目
- hdu4221Greedy? 贪心
- PHP代码规范
- 【JavaSE】day14_线程安全_synchroized关键字
- MySQL分表与分区
- 随机样本一致抽样
- ASP.NET应用程序与页面生命周期
- 《你不知道的JavaScript》读书笔记(二)词法作用域
- android安全机制
- RAC修改VIP地址
- js中的Error对象
- JavaScript中的继承
- css中背景 字体 文体属性练习
- iOS-plist的保存和读取
- 如何判断不是微信内置浏览器
- Socket层实现系列 — send()类发送函数的实现
- 301永久重定向实现方式及302重定向
- 方便快捷读取手机联系人