操作环境:python2.7+scrapy

安装比较简单,网上教程也超多,就不在此赘述。
示例网站:https://www.cnblogs.com/cate/python/ (爬去关于安科网所有python的帖子)
#############开始新建项目 E:work\scrapy_pro\
打开cmd(命令行窗口)

cd进入文件夹 cd..回到上一级 进入某个盘符直接输入E: 大小写没关系
############执行新建项目命令 项目名cnblog
scrapy startproject cnblog

创建ok,进入项目cd cnblog ,操作该项目时在这一层目录。
目录如下

dir-spiders:存放所有spider的文件,里面的spider文件自己新建。
items.py:用来存放爬去的内容,在导出数据时需要用到
middlewares.py:中间件文件,写入多余的功能,比如需要与PhantomJs结合使用时修改这里。
pipelines.py:暂时没用过。
settings.py:项目配置文件。
######打开spiders文件夹开始新建spider文件 test.py 名字无所谓

打开test.py
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule<br />###引入的是items.py里面构造的类
from cnblog.items import ExampleItem<br />class TestSpider(CrawlSpider):<br />
name = 'blog'
allowed_domains = ['cnblogs.com']
start_urls = ['https://www.cnblogs.com/cate/python/']
rules = (
###爬去索引页并跟踪其中链接
###查找start_urls 所有的分页页面
Rule(LinkExtractor(allow=r'/cate/python/[2-9]*'), follow=True),
###爬去items页面并将下载响应返回个头parse_item函数
####查询每个分页页面的详情页
Rule(LinkExtractor(allow=r'http://www.cnblogs.com/[a-z]*/p/[0-1]*'), callback='parse_item', follow=False
),
)
def parse_item(self, response):
item = ExampleItem()
#i['domain_id'] = response.xpath('//input[@id="sid"]/@value').extract()
#i['name'] = response.xpath('//div[@id="name"]').extract()
#i['description'] = response.xpath('//div[@id="description"]').extract()
#print response.xpath('//a[@id="Header1_HeaderTitle"]/text()').extract()
if response.xpath('//a[@id="Header1_HeaderTitle"]/text()').extract():
item['blog_name'] = response.xpath('//a[@id="Header1_HeaderTitle"]/text()').extract()
else:
item['blog_name'] = 'null'
items = []
items.append(item)
return itemsitems.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 ExampleItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
blog_name = scrapy.Field()上面是两个需要用到的两个文件
前面的是spider文件,另一个是定义存储的内容文件
items.py
blog_name = scrapy.Field() //定义一个获取的字段,获取详情页的标题
test.py
from scrapy.linkextractors import LinkExtractor ##引入linkextractors 用于筛选链接和跟进链接,还有很多功能,可以去百度下
from scrapy.spiders import CrawlSpider, Rule ##定义spider的模板,引入Rule规则
from example.items import ExampleItem ##引入定义的items.py
下面是对类的详细介绍
class TestSpider(CrawlSpider): ##继承模板CrawlSpider 普通模板继承Spider
name = 'blog' ###定义spider名 运行---$ scrapy crawl blog
allowed_domains = ['cnblogs.com'] ## 定义查找范围
start_urls = ['https://www.cnblogs.com/cate/python/'] ###初始url
###通过rules限定查找的url
###分页的url ='/cate/python/[2-9]*'
###详情页的url = 'http://www.cnblogs.com/[a-z]*/p/[0-1]*'
####当有follow=True 则会跟进该页面
####原理就是 spider在初始页面查找,同时查找帖子详情页的url和下一个分页,同时跟进下一个分页页面,继续查找下一个分页页面和上面的详情页url,详情页面使用回调函数进行采集
rules = (
###爬去索引页并跟踪其中链接
###查找start_urls 所有的分页页面
Rule(LinkExtractor(allow=r'/cate/python/[2-9]*'), follow=True),
###爬去items页面并将下载响应返回个头parse_item函数
####查询每个分页页面的详情页
Rule(LinkExtractor(allow=r'http://www.cnblogs.com/[a-z]*/p/[0-1]*'), callback='parse_item', follow=False
),
)
####详情页面回调函数
def parse_item(self, response):
###实例化item类
item = ExampleItem()
###几种xpath获取标签的方式
#i['domain_id'] = response.xpath('//input[@id="sid"]/@value').extract()
#i['name'] = response.xpath('//div[@id="name"]').extract()
#i['description'] = response.xpath('//div[@id="description"]').extract()
#print response.xpath('//a[@id="Header1_HeaderTitle"]/text()').extract()
if response.xpath('//a[@id="Header1_HeaderTitle"]/text()').extract():
item['blog_name'] = response.xpath('//a[@id="Header1_HeaderTitle"]/text()').extract()
else:
item['blog_name'] = 'null'
items = []
###把数据装进仓库
items.append(item)
return items#####现在开始执行spider
切换到当前目录

—————————导出json文件

scrapy crawl blog-o data.json -t json-s LOG_LEVEl=ERROR
注意:执行的是spider 的name, 后面的内容是设置导出文件的类型,名字以及log的级别,设置为error级别。
下一次教大家怎么配合PhantomJS来写爬虫。
我的邮箱是[email protected]有兴趣的同学可以一起交流,非诚勿扰。