目标:爬取全国报刊名称及地址

链接:http://news.xinhuanet.com/zgjx/2007-09/13/content_6714741.htm

目的:练习scrapy爬取数据

学习过scrapy的基本使用方法后,我们开始写一个最简单的爬虫吧。

目标截图:

  1、创建爬虫工程

  1. $ cd ~/code/crawler/scrapyProject
  2. $ scrapy startproject newSpapers

  2、创建爬虫程序

  1. $ cd newSpapers/
  2. $ scrapy genspider nationalNewspaper news.xinhuanet.com 

  3、配置数据爬取项 

  1. $ cat items.py
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Define here the models for your scraped items
  5. #
  6. # See documentation in:
  7. # http://doc.scrapy.org/en/latest/topics/items.html
  8.  
  9. import scrapy
  10.  
  11. class NewspapersItem(scrapy.Item):
  12. # define the fields for your item here like:
  13. # name = scrapy.Field()
  14. name = scrapy.Field()
  15. addr = scrapy.Field()

 4、 配置爬虫程序

  1. $ cat spiders/nationalNewspaper.py
  2. # -*- coding: utf-8 -*-
  3. import scrapy
  4. from newSpapers.items import NewspapersItem
  5.  
  6. class NationalnewspaperSpider(scrapy.Spider):
  7. name = "nationalNewspaper"
  8. allowed_domains = ["news.xinhuanet.com"]
  9. start_urls = ['http://news.xinhuanet.com/zgjx/2007-09/13/content_6714741.htm']
  10.  
  11. def parse(self, response):
  12. sub_country = response.xpath('//*[@id="Zoom"]/div/table/tbody/tr[2]')
  13. sub2_local = response.xpath('//*[@id="Zoom"]/div/table/tbody/tr[4]')
  14. tags_a_country = sub_country.xpath('./td/table/tbody/tr/td/p/a')
  15. items = []
  16. for each in tags_a_country:
  17. item = NewspapersItem()
  18. item['name'] = each.xpath('./strong/text()').extract()
  19. item['addr'] = each.xpath('./@href').extract()
  20. items.append(item)
  21. return items

  5、配置谁去处理爬取结果

  1. $ cat settings.py
  2. ……
  3. #HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
  4. ITEM_PIPELINES = {'newSpapers.pipelines.NewspapersPipeline':100}

  6、配置数据处理程序

  1. $ cat pipelines.py
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Define your item pipelines here
  5. #
  6. # Don't forget to add your pipeline to the ITEM_PIPELINES setting
  7. # See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
  8.  
  9. import time
  10. class NewspapersPipeline(object):
  11. def process_item(self, item, spider):
  12. now = time.strftime('%Y-%m-%d',time.localtime())
  13. filename = 'newspaper.txt'
  14. print '================='
  15. print item
  16. print '================'
  17. with open(filename,'a') as fp:
  18. fp.write(item['name'][0].encode("utf8")+ '\t' +item['addr'][0].encode("utf8") + '\n')
  19. return item

  7、查看结果

  1. $ cat spiders/newspaper.txt
  2. 人民日报 http://paper.people.com.cn/rmrb/html/2007-09/20/node_17.htm
  3. 海外版 http://paper.people.com.cn/rmrbhwb/html/2007-09/20/node_34.htm
  4. 光明日报 http://www.gmw.cn/01gmrb/2007-09/20/default.htm
  5. 经济日报 http://www.economicdaily.com.cn/no1/
  6. 解放军报 http://www.gmw.cn/01gmrb/2007-09/20/default.htm
  7. 中国日报 http://pub1.chinadaily.com.cn/cdpdf/cndy/

  

程序源代码:

scrapy实战--爬取报刊名称及地址的更多相关文章

  1. 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息

    简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 系统环境:Fedora22(昨天已安装scrapy环境) 爬取的开始URL:ht ...

  2. 教程+资源,python scrapy实战爬取知乎最性感妹子的爆照合集(12G)!

    一.出发点: 之前在知乎看到一位大牛(二胖)写的一篇文章:python爬取知乎最受欢迎的妹子(大概题目是这个,具体记不清了),但是这位二胖哥没有给出源码,而我也没用过python,正好顺便学一学,所以 ...

  3. scrapy实战--爬取最新美剧

    现在写一个利用scrapy爬虫框架爬取最新美剧的项目. 准备工作: 目标地址:http://www.meijutt.com/new100.html 爬取项目:美剧名称.状态.电视台.更新时间 1.创建 ...

  4. Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)

    1. Scrapy框架 Scrapy是python下实现爬虫功能的框架,能够将数据解析.数据处理.数据存储合为一体功能的爬虫框架. 2. Scrapy安装 1. 安装依赖包 yum install g ...

  5. Scrapy实战篇(六)之Scrapy配合Selenium爬取京东信息(上)

    在之前的一篇实战之中,我们已经爬取过京东商城的文胸数据,但是前面的那一篇其实是有一个缺陷的,不知道你看出来没有,下面就来详细的说明和解决这个缺陷. 我们在京东搜索页面输入关键字进行搜索的时候,页面的返 ...

  6. scrapy实战2分布式爬取lagou招聘(加入了免费的User-Agent随机动态获取库 fake-useragent 使用方法查看:https://github.com/hellysmile/fake-useragent)

    items.py # -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentati ...

  7. scrapy框架爬取笔趣阁完整版

    继续上一篇,这一次的爬取了小说内容 pipelines.py import csv class ScrapytestPipeline(object): # 爬虫文件中提取数据的方法每yield一次it ...

  8. scrapy框架爬取笔趣阁

    笔趣阁是很好爬的网站了,这里简单爬取了全部小说链接和每本的全部章节链接,还想爬取章节内容在biquge.py里在加一个爬取循环,在pipelines.py添加保存函数即可 1 创建一个scrapy项目 ...

  9. Python分布式爬虫开发搜索引擎 Scrapy实战视频教程

    点击了解更多Python课程>>> Python分布式爬虫开发搜索引擎 Scrapy实战视频教程 课程目录 |--第01集 教程推介 98.23MB |--第02集 windows下 ...

随机推荐

  1. GODADDY 优质DNS 未被墙

    下面列出的是我本地测试出的优质服务器 NS10.DOMAINCONTROL.COM NS12.DOMAINCONTROL.COM NS14.DOMAINCONTROL.COM NS19.DOMAINC ...

  2. IE中的userData

    之前做项目时用到了localstorage,但是考虑到浏览器存在IE8以下不兼容问题,所以来介绍以下IE中的userData. 本地存储解决方案很多,比如Flash SharedObject.Goog ...

  3. Java_反射机制详解

    本篇文章依旧采用小例子来说明,因为我始终觉的,案例驱动是最好的,要不然只看理论的话,看了也不懂,不过建议大家在看完文章之后,在回过头去看看理论,会有更好的理解. 下面开始正文. [案例1]通过一个对象 ...

  4. Flow类中的resolveBreaks与resolveContinues

    /** Resolve all continues of this statement. */ boolean resolveContinues(JCTree tree) { boolean resu ...

  5. nodejs + express 热更新

    以前node中的express框架,每次修改代码之后,都需要重新npm start 才能看到改动的效果,非常麻烦,所以这里引入nodemon模块,实现了不用重启也能自动更新这样的好处 1.全局安装no ...

  6. celery在Django中的应用

    这里不解释celery,如果不清楚可以参考下面链接: http://docs.celeryproject.org/en/latest/getting-started/introduction.html ...

  7. C语言理论知识

    C语言-----理论部分   一:软件开发概述1.程序语言的发展:机器语言-->汇编语言-->高级语言.2.软件开发的基本步骤与方法:分析问题,建立数学模型-->确定数据结构和算法- ...

  8. spring cloud连载第三篇之Spring Cloud Netflix

    1. Service Discovery: Eureka Server(服务发现:eureka服务器) 1.1 依赖 <dependency> <groupId>org.spr ...

  9. MySQL 学习 --- 数据结构和索引

    本文参考了多篇文章集成的笔记,希望各位学习之前可以阅读以下参考资料先 概述 文章分几个部分 :第一部分介绍了B-Tree 和 B+Tree 这种数据结构作为索引:第二部分介绍索引的最左前缀原则和覆盖索 ...

  10. jQuery如何根据元素值删除数组元素

    用到的方法$.inArry(); $.inArray( value, array [, fromIndex ] ) value 任意类型 用于查找的值. array Array类型 指定被查找的数组. ...