阳光热线问政平台

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之案例一的更多相关文章

  1. 爬虫框架Scrapy之案例二

    新浪网分类资讯爬虫 爬取新浪网导航页所有下所有大类.小类.小类里的子链接,以及子链接页面的新闻内容. 效果演示图: items.py import scrapy import sys reload(s ...

  2. 爬虫框架Scrapy之案例三图片下载器

    items.py class CoserItem(scrapy.Item): url = scrapy.Field() name = scrapy.Field() info = scrapy.Fiel ...

  3. 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神

    本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http://www.xiaohuar.com/,让你体验爬取校花的成就感. Scr ...

  4. 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神

    原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...

  5. 爬虫框架Scrapy

    前面十章爬虫笔记陆陆续续记录了一些简单的Python爬虫知识, 用来解决简单的贴吧下载,绩点运算自然不在话下. 不过要想批量下载大量的内容,比如知乎的所有的问答,那便显得游刃不有余了点. 于是乎,爬虫 ...

  6. 第三篇:爬虫框架 - Scrapy

    前言 Python提供了一个比较实用的爬虫框架 - Scrapy.在这个框架下只要定制好指定的几个模块,就能实现一个爬虫. 本文将讲解Scrapy框架的基本体系结构,以及使用这个框架定制爬虫的具体步骤 ...

  7. 网络爬虫框架Scrapy简介

    作者: 黄进(QQ:7149101) 一. 网络爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本:它是一个自动提取网页的程序,它为搜索引擎从万维 ...

  8. Linux 安装python爬虫框架 scrapy

    Linux 安装python爬虫框架 scrapy http://scrapy.org/ Scrapy是python最好用的一个爬虫框架.要求: python2.7.x. 1. Ubuntu14.04 ...

  9. Python爬虫框架Scrapy实例(三)数据存储到MongoDB

    Python爬虫框架Scrapy实例(三)数据存储到MongoDB任务目标:爬取豆瓣电影top250,将数据存储到MongoDB中. items.py文件复制代码# -*- coding: utf-8 ...

随机推荐

  1. A Secure Cookie Protocol 安全cookie协议 配置服务器Cookie

    Title http://www.cse.msu.edu/~alexliu/publications/Cookie/cookie.pdf AbstractCookies are the primary ...

  2. HOW TO FIX "EXPECTED BEGIN_ARRAY BUT WAS BEGIN_OBJECT" IN RETROFIT ?

    https://www.freshbytelabs.com/2018/05/how-to-fix-expected-beginarray-but-was.html HOW TO FIX "E ...

  3. Spark Streaming源码分析 – InputDStream

    对于NetworkInputDStream而言,其实不是真正的流方式,将数据读出来后不是直接去处理,而是先写到blocks中,后面的RDD再从blocks中读取数据继续处理这就是一个将stream离散 ...

  4. 基于HTTP协议的轻量级开源简单队列服务:HTTPSQS 笔记

    队列服务就是为了提高相应速度,把耗时或者不需要即时处理的流程放到异步处理过程中,HTTPSQS就是这样一个服务. 更详细的可以参考 http://blog.s135.com/httpsqs/,这里记录 ...

  5. 剑指Offer——二进制中1的个数

    题目描述: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 分析: 加入一个数的二进制位是XXX...XXX1000...000,那么这个数减去1,就会变成XXX...XXX0111 ...

  6. app瘦身和包压缩技术有什么区别?

    APP瘦身 针对app文件中的文件进行优化,利用素材的拉伸,祛除不必要的文件,优化png, jpg素材,压缩音视频素材等方式实现app文件的减小. 包压缩技术 所谓包压缩,顾名思义就是将手游的安装包体 ...

  7. VUE的安装与Django之间打通数据

    一  VUE的安装与项目创建 1.1.安装nodeJS 官网下载安装:https://nodejs.org/zh-cn/ 1.2.安装脚手架 vue官网 => 学习 => 教程 => ...

  8. 21.如何将java类对象转化为json字符串

    使用阿里巴巴的fastJson 下载链接: 链接: https://pan.baidu.com/s/1dHjLOm1 密码: rr3w 用法如下: User user = new User(); us ...

  9. JAVA math包

    Math类: java.lang.Math 类中包含基本的数字操作,如指数.对数.平方根和三角函数. java.math是一个包,提供用于执行任意精度整数(BigInteger)算法和任意精度小数(B ...

  10. ajax参数补充

    ajax参数补充 contentType 当我们使用form表单提交数据时,有一个enctype属性,默认情况下不写 此时我们提交数据时,会默认将数据以application/x-www-form-u ...