scrapy实战--爬取报刊名称及地址
目标:爬取全国报刊名称及地址
链接:http://news.xinhuanet.com/zgjx/2007-09/13/content_6714741.htm
目的:练习scrapy爬取数据
学习过scrapy的基本使用方法后,我们开始写一个最简单的爬虫吧。
目标截图:

1、创建爬虫工程
$ cd ~/code/crawler/scrapyProject
$ scrapy startproject newSpapers
2、创建爬虫程序
$ cd newSpapers/
$ scrapy genspider nationalNewspaper news.xinhuanet.com
3、配置数据爬取项
$ cat items.py
# -*- 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 class NewspapersItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
name = scrapy.Field()
addr = scrapy.Field()
4、 配置爬虫程序
$ cat spiders/nationalNewspaper.py
# -*- coding: utf-8 -*-
import scrapy
from newSpapers.items import NewspapersItem class NationalnewspaperSpider(scrapy.Spider):
name = "nationalNewspaper"
allowed_domains = ["news.xinhuanet.com"]
start_urls = ['http://news.xinhuanet.com/zgjx/2007-09/13/content_6714741.htm'] def parse(self, response):
sub_country = response.xpath('//*[@id="Zoom"]/div/table/tbody/tr[2]')
sub2_local = response.xpath('//*[@id="Zoom"]/div/table/tbody/tr[4]')
tags_a_country = sub_country.xpath('./td/table/tbody/tr/td/p/a')
items = []
for each in tags_a_country:
item = NewspapersItem()
item['name'] = each.xpath('./strong/text()').extract()
item['addr'] = each.xpath('./@href').extract()
items.append(item)
return items
5、配置谁去处理爬取结果
$ cat settings.py
……
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
ITEM_PIPELINES = {'newSpapers.pipelines.NewspapersPipeline':100}
6、配置数据处理程序
$ cat pipelines.py
# -*- coding: utf-8 -*- # Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html import time
class NewspapersPipeline(object):
def process_item(self, item, spider):
now = time.strftime('%Y-%m-%d',time.localtime())
filename = 'newspaper.txt'
print '================='
print item
print '================'
with open(filename,'a') as fp:
fp.write(item['name'][0].encode("utf8")+ '\t' +item['addr'][0].encode("utf8") + '\n')
return item
7、查看结果
$ cat spiders/newspaper.txt
人民日报 http://paper.people.com.cn/rmrb/html/2007-09/20/node_17.htm
海外版 http://paper.people.com.cn/rmrbhwb/html/2007-09/20/node_34.htm
光明日报 http://www.gmw.cn/01gmrb/2007-09/20/default.htm
经济日报 http://www.economicdaily.com.cn/no1/
解放军报 http://www.gmw.cn/01gmrb/2007-09/20/default.htm
中国日报 http://pub1.chinadaily.com.cn/cdpdf/cndy/
程序源代码:
scrapy实战--爬取报刊名称及地址的更多相关文章
- 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息
简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 系统环境:Fedora22(昨天已安装scrapy环境) 爬取的开始URL:ht ...
- 教程+资源,python scrapy实战爬取知乎最性感妹子的爆照合集(12G)!
一.出发点: 之前在知乎看到一位大牛(二胖)写的一篇文章:python爬取知乎最受欢迎的妹子(大概题目是这个,具体记不清了),但是这位二胖哥没有给出源码,而我也没用过python,正好顺便学一学,所以 ...
- scrapy实战--爬取最新美剧
现在写一个利用scrapy爬虫框架爬取最新美剧的项目. 准备工作: 目标地址:http://www.meijutt.com/new100.html 爬取项目:美剧名称.状态.电视台.更新时间 1.创建 ...
- Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)
1. Scrapy框架 Scrapy是python下实现爬虫功能的框架,能够将数据解析.数据处理.数据存储合为一体功能的爬虫框架. 2. Scrapy安装 1. 安装依赖包 yum install g ...
- Scrapy实战篇(六)之Scrapy配合Selenium爬取京东信息(上)
在之前的一篇实战之中,我们已经爬取过京东商城的文胸数据,但是前面的那一篇其实是有一个缺陷的,不知道你看出来没有,下面就来详细的说明和解决这个缺陷. 我们在京东搜索页面输入关键字进行搜索的时候,页面的返 ...
- 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 ...
- scrapy框架爬取笔趣阁完整版
继续上一篇,这一次的爬取了小说内容 pipelines.py import csv class ScrapytestPipeline(object): # 爬虫文件中提取数据的方法每yield一次it ...
- scrapy框架爬取笔趣阁
笔趣阁是很好爬的网站了,这里简单爬取了全部小说链接和每本的全部章节链接,还想爬取章节内容在biquge.py里在加一个爬取循环,在pipelines.py添加保存函数即可 1 创建一个scrapy项目 ...
- Python分布式爬虫开发搜索引擎 Scrapy实战视频教程
点击了解更多Python课程>>> Python分布式爬虫开发搜索引擎 Scrapy实战视频教程 课程目录 |--第01集 教程推介 98.23MB |--第02集 windows下 ...
随机推荐
- UBUNTU下MONGODB出现PHP Fatal error: Uncaught exception 'MongoConnectionException' with message 和 Authentication failed on database 'admin' with username
MONGO 远程连接服务器,出现: PHP Fatal error: Uncaught exception Stack trace:# /var/www/data/update_data.php(): ...
- C/C++ -- Gui编程 -- Qt库的使用 -- 对话框QDialog
模态对话框 -----源文件main.cpp(工程QtDialog)----- #include "qtdialog.h" #include <QApplication> ...
- 回退Ubuntu记录
前言 由于Ubuntu18经常出错,因而决定回退Ubuntu16,下面是记录回退问题及美化,以便以后需要. 问题总结 磁盘挂载 挂载其他磁盘分区时,提示错误"Metadata kept in ...
- Hadoop+Hive 操作mongodb数据
Hadoop+Hive 操作mongodb数据 1.版本概述 hadoop-2.7.3.hive-2.2 下载响应的jar包:http://mvnrepository.com/,直接搜索想要的jar包 ...
- Python List 基础学习
list&tuple&dict list list 常见操作 初始化: list1 = [123, 'abc', 4.56, ['inner', 'list'], 7-9j] list ...
- 不会几个框架,都不好意思说搞过前端: Node.js & angular.js
Node.js 菜鸟教程 :http://www.runoob.com/nodejs/nodejs-install-setup.html angular.js 菜鸟教程 :http://www.r ...
- SSH(Struts 2.3.31 + Spring 4.1.6 + Hibernate 5.0.12 + Ajax)框架整合实现简单的增删改查(包含分页,Ajax 无刷新验证该用户是否存在)
软件152 余建强 该文将以员工.部门两表带领大家进入SSH的整合教程: 源码下载:http://download.csdn.net/detail/qq_35318576/9877235 SSH 整合 ...
- 【转】marquee标签简介
本文转自:http://www.360doc.com/content/12/0818/16/8351655_230872993.shtml marquee语法 <marquee>&l ...
- C#基础知识回顾--BackgroundWorker介绍
简介 BackgroundWorker是.net里用来执行多线程任务的控件,它允许编程者在一个单独的线程上执行一些操作.耗时的操作(如下载和数据库事务)在长时间运行时可能会导致用户界面 (UI) 始终 ...
- 【模板】Bellman—Fort 单源最短路径算法
2333 适用于边集储存 #include<bits/stdc++.h> using namespace std; const int inf=0x3fffffff; ],t[],d[], ...