Python实现爬取阳光热线问政平台

发布 : 2017-04-22 分类 : Python 浏览 :

需求

1
2
3
http://wz.sun0769.com/index.php/question/questionType?type=4

爬取投诉帖子的编号、帖子的url、帖子的标题,和帖子里的内容。

新建Scrapy项目

1
scrapy startproject Dongguanspider

创建爬虫

1
scrapy genspider dongguan "wz.sun0769.com"

items.py

1
2
3
4
5
6
7
8
9
10
# -*- coding: utf-8 -*-

import scrapy


class DongguanspiderItem(scrapy.Item):
id = scrapy.Field() # 投诉帖子的编号
url = scrapy.Field() # 投诉帖子的url
title = scrapy.Field() # 投诉帖子的标题
content = scrapy.Field() # 投诉帖子的内容

settings.py

1
2
3
4
5
6
7
ITEM_PIPELINES = {
'Dongguanspider.pipelines.DongguanspiderPipeline': 300,
}

# 日志文件名和处理等级
LOG_FILE = "dg.log"
LOG_LEVEL = "DEBUG"

pipelines.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# -*- coding: utf-8 -*-

# 文件处理类库,可以指定编码格式
import codecs
import json


class DongguanspiderPipeline(object):
def __init__(self):
# 创建一个只写文件,指定文本编码格式为utf-8
self.filename = codecs.open('dongguan.json', 'w', encoding='utf-8')

def process_item(self, item, spider):
content = json.dumps(dict(item), ensure_ascii=False) + "\n"
self.filename.write(content)
return item

def spider_closed(self, spider):
self.file.close()

main.py

1
2
3
4
# -*- coding: utf-8 -*-

from scrapy import cmdline
cmdline.execute('scrapy crawl dongguan'.split())

Spider版本

dongguan.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -*- coding: utf-8 -*-
import scrapy
from Dongguanspider.items import DongguanspiderItem


class DongguanSpider(scrapy.Spider):
name = "dongguan"
allowed_domains = ["wz.sun0769.com"]
url = 'http://wz.sun0769.com/index.php/question/questionType?type=4&page='
offset = 0
start_urls = [url + str(offset)]

def parse(self, response):
# 取出每个页面中的帖子链接列表
links = response.xpath('//*[@class="greyframe"]/table[2]//a[@class="news14"]/@href').extract()
# 迭代发送每个帖子的请求,调用parse_item方法处理
for link in links:
yield scrapy.Request(link, callback=self.parse_item)
# 设置页码终止条件,并且每次发送新的页面请求调用parse方法处理
if self.offset <= 71130:
self.offset += 30
yield scrapy.Request(self.url + str(self.offset), callback=self.parse)

# 处理每个帖子里
def parse_item(self, response):
item = DongguanspiderItem()
# 标题
item['title'] = response.xpath('//div[contains(@class,"pagecenter p3")]//strong/text()').extract()[0].strip()
# 编号
item['id'] = item['title'].split(" ")[1].split(":")[1].split(":")[1].strip()
# 链接
item['url'] = response.url.strip()
# 内容
content = response.xpath('//div[@class="contentext"]/text()').extract()
if len(content) == 0:
content = response.xpath('//div[@class="c1 text14_2"]/text()').extract()
# content为列表,通过join方法拼接为字符串,并去除首尾空格
item['content'] = "".join(content).strip()
else:
item['content'] = "".join(content).strip()
yield item

Markdown

Markdown

CrawlSpider版本

本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2017/04/22/Python实现爬取阳光热线问政平台数据/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹