爬虫框架Scrapy之案例一
阳光热线问政平台
http://wz.sun0769.com/index.php/question/questionType?type=4
爬取投诉帖子的编号、帖子的url、帖子的标题,和帖子里的内容。
items.py
import scrapy
class SunwzItem(scrapy.Item):
number = scrapy.Field()
url = scrapy.Field()
title = scrapy.Field()
content = scrapy.Field()
spiders/sunwz.py
# -*- coding: utf-8 -*-
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from Sunwz.items import SunwzItem
class SunwzSpider(CrawlSpider):
name = 'sunwz'
num = 0
allow_domain = ['http://wz.sun0769.com/']
start_urls = ['http://wz.sun0769.com/index.php/question/questionType?type=4']
rules = {
Rule(LinkExtractor(allow='page')),
Rule(LinkExtractor(allow='/index\.php/question/questionType\?type=4$')),
Rule(LinkExtractor(allow='/html/question/\d+/\d+\.shtml$'), follow = True, callback='parse_content')
}
xpathDict = {
'title': '//div[contains(@class, "pagecenter p3")]/div/div/div[contains(@class,"cleft")]/strong/text()',
'content': '//div[contains(@class, "c1 text14_2")]/text()',
'content_first': '//div[contains(@class, "contentext")]/text()'
}
def parse_content(self, response):
item = SunwzItem()
content = response.xpath(self.xpathDict['content_first']).extract()
if len(content) == 0:
content = response.xpath(self.xpathDict['content']).extract()[0]
else:
content = content[0]
title = response.xpath(self.xpathDict['title']).extract()[0]
title_list = title.split(' ')
number = title_list[-1]
number = number.split(':')[-1]
url = response.url
item['url'] = url
item['number'] = number
item['title'] = title
item['content'] = content
yield item
pipelines.py
import json
import codecs
class JsonWriterPipeline(object):
def __init__(self):
self.file = codecs.open('sunwz.json', 'w', encoding='utf-8')
def process_item(self, item, spider):
line = json.dumps(dict(item), ensure_ascii=False) + "\n"
self.file.write(line)
return item
def spider_closed(self, spider):
self.file.close()
settings.py
ITEM_PIPELINES = {
'Sunwz.pipelines.JsonWriterPipeline': 300,
}
在项目根目录下新建main.py文件,用于调试
from scrapy import cmdline
cmdline.execute('scrapy crawl sunwz'.split())
执行程序
py2 main.py
爬虫框架Scrapy之案例一的更多相关文章
- 爬虫框架Scrapy之案例二
新浪网分类资讯爬虫 爬取新浪网导航页所有下所有大类.小类.小类里的子链接,以及子链接页面的新闻内容. 效果演示图: items.py import scrapy import sys reload(s ...
- 爬虫框架Scrapy之案例三图片下载器
items.py class CoserItem(scrapy.Item): url = scrapy.Field() name = scrapy.Field() info = scrapy.Fiel ...
- 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http://www.xiaohuar.com/,让你体验爬取校花的成就感. Scr ...
- 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...
- 爬虫框架Scrapy
前面十章爬虫笔记陆陆续续记录了一些简单的Python爬虫知识, 用来解决简单的贴吧下载,绩点运算自然不在话下. 不过要想批量下载大量的内容,比如知乎的所有的问答,那便显得游刃不有余了点. 于是乎,爬虫 ...
- 第三篇:爬虫框架 - Scrapy
前言 Python提供了一个比较实用的爬虫框架 - Scrapy.在这个框架下只要定制好指定的几个模块,就能实现一个爬虫. 本文将讲解Scrapy框架的基本体系结构,以及使用这个框架定制爬虫的具体步骤 ...
- 网络爬虫框架Scrapy简介
作者: 黄进(QQ:7149101) 一. 网络爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本:它是一个自动提取网页的程序,它为搜索引擎从万维 ...
- Linux 安装python爬虫框架 scrapy
Linux 安装python爬虫框架 scrapy http://scrapy.org/ Scrapy是python最好用的一个爬虫框架.要求: python2.7.x. 1. Ubuntu14.04 ...
- Python爬虫框架Scrapy实例(三)数据存储到MongoDB
Python爬虫框架Scrapy实例(三)数据存储到MongoDB任务目标:爬取豆瓣电影top250,将数据存储到MongoDB中. items.py文件复制代码# -*- coding: utf-8 ...
随机推荐
- 2017 Multi-University Training Contest - Team 7
HDU6121 Build a tree 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题目意思:一棵 n 个点的完全 k 叉树,结点标号从 ...
- python基础-第八篇-8.1初识Socket
socket基础 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. so ...
- Again Array Queries---Lightoj1100(循环暴力)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1100 题意是给你n个数,q个询问,每次求出 a 到 b(从0开始)最小差值: 直接暴力 ...
- 转:docker的核心技术深度剖析
一.docker是什么 Docker的英文本意是码头工人,也就是搬运工,这种搬运工搬运的是集装箱(Container),集装箱里面装的可不是商品货物,而是任意类型的App,Docker把App(叫Pa ...
- Linux升级python至3.4.4
wget https://www.python.org/ftp/python/3.4.4/Python-3.4.4.tgz ls .tgz mkdir /usr/local/python3 cd Py ...
- MySQL存储引擎与事务
1.作用 和磁盘的数据打交道 2.简介 MySQL 基于存储引擎管理 表空间数据数据文件 3.种类 Innodb存储引擎ibd:存储表的数据行和索引frm:表基本结构信息Myisam存储引擎frmm ...
- LayoutInflater的动态增加控件
在实际开发中LayoutInflater这个类是非常有用的,它的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件. 而findView ...
- Java设计模式-工厂方法模式(Virtual Constructor/Polymorphic Factory)
工厂方法模式(Virtual Constructor/Polymorphic Factory) 工厂方法模式是类的创建模式,又叫做虚拟构造子模式(Virtual Constructor)或者多态性工厂 ...
- 打开的文件符合PDF/A标准,且已在只读模式下打开,以防被修改
PDF/A是一种用于长期归档和保留电子文档的ISO标准.您扫描到PDF的文档符合PDF/A的规范.您可以指定是否要用本查看模式查看文档. PDF/A 问题提示:“打开的文件符合PDF/A标准,且已在只 ...
- python全栈开发从入门到放弃之面向对象反射
1.classmethod.staticmethod方法 classmethod类方法默认参数cls,可以直接用类名调用,可以与类属性交互 #student文件内容 宝宝,男 博博,女 海娇,男 海燕 ...