scrapy框架是个比较简单易用基于python的爬虫框架,http://scrapy-chs.readthedocs.org/zh_CN/latest/ 这个是不错的中文文档

  几个比较重要的部分:

  items.py:用来定义需要保存的变量,其中的变量用Field来定义,有点像python的字典

  pipelines.py:用来将提取出来的Item进行处理,处理过程按自己需要进行定义

  spiders:定义自己的爬虫

  爬虫的类型也有好几种:

  1)spider:最基本的爬虫,其他的爬虫一般是继承了该最基本的爬虫类,提供访问url,返回response的功能,会默认调用parse方法

  2)CrawlSpider:继承spider的爬虫,实际使用比较多,设定rule规则进行网页的跟进与处理, 注意点:编写爬虫的规则的时候避免使用parse名,因为这会覆盖继承的spider的的方法parse造成错误。   其中比较重要的是对Rule的规则的编写,要对具体的网页的情况进行分析。

  3)XMLFeedSpider 与 CSVFeedSpider

  实际操作:

items.py下的:

from scrapy.item import Item, Field

class Website(Item):

    headTitle = Field()
description = Field()
url = Field()

spider.py下的:

# -*- coding:gb2312 -*-
from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector
from dirbot.items import Website
import sys
import string
sys.stdout=open('output.txt','w') #将打印信息输出在相应的位置下 add = 0
class DmozSpider(CrawlSpider): name = "huhu"
allowed_domains = ["cnblogs.com"]
start_urls = [
"http://www.cnblogs.com/huhuuu",
] rules = (
# 提取匹配 huhuuu/default.html\?page\=([\w]+) 的链接并跟进链接(没有callback意味着follow默认为True)
Rule(SgmlLinkExtractor(allow=('huhuuu/default.html\?page\=([\w]+)', ),)), # 提取匹配 'huhuuu/p/' 的链接并使用spider的parse_item方法进行分析
Rule(SgmlLinkExtractor(allow=('huhuuu/p/', )), callback='parse_item'),
) def parse_item(self, response):
global add #用于统计博文的数量 print add
add+=1 sel = Selector(response)
items = [] item = Website()
item['headTitle'] = sel.xpath('/html/head/title/text()').extract()#观察网页对应得html源码
item['url'] = response
print item
items.append(item)
return items

最后在相应的目录文件下运行scrapy crawl huhu

结果:

但是我的博文好歹有400篇左右,最后只搜出了100篇,这是什么情况

查了一些搜出来的网页地址,很多都是2013.10 到最近更新的博文情况,没道理啊,最后注意了老的博文的网址,原来老的博文地址的结构更新的博文地址的结构不同

现在的:http://www.cnblogs.com/huhuuu/p/3384978.html

老的:http://www.cnblogs.com/huhuuu/archive/2012/04/10/2441060.html

然后在rule里面加入老网页的规则,就可以把博客中没加密的博文都搜出来了

# -*- coding:gb2312 -*-
from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector
from dirbot.items import Website
import sys
import string
sys.stdout=open('output.txt','w') #将打印信息输出在相应的位置下 add = 0
class DmozSpider(CrawlSpider): name = "huhu"
allowed_domains = ["cnblogs.com"]
start_urls = [
"http://www.cnblogs.com/huhuuu",
] rules = (
# 提取匹配 huhuuu/default.html\?page\=([\w]+) 的链接并跟进链接(没有callback意味着follow默认为True)
Rule(SgmlLinkExtractor(allow=('huhuuu/default.html\?page\=([\w]+)', ),)), # 提取匹配 'huhuuu/p/' 的链接并使用spider的parse_item方法进行分析
Rule(SgmlLinkExtractor(allow=('huhuuu/p/', )), callback='parse_item'),
Rule(SgmlLinkExtractor(allow=('huhuuu/archive/', )), callback='parse_item'), #以前的一些博客是archive形式的所以
) def parse_item(self, response):
global add #用于统计博文的数量 print add
add+=1 sel = Selector(response)
items = [] item = Website()
item['headTitle'] = sel.xpath('/html/head/title/text()').extract()#观察网页对应得html源码
item['url'] = response
print item
items.append(item)
return items

又做了一个爬取博客园首页博客的代码,其实只要修改Rule即可:

# -*- coding:gb2312 -*-
from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector
from dirbot.items import Website
import sys
import string
sys.stdout=open('output.txt','w') #将打印信息输出在相应的位置下 add = 0
class DmozSpider(CrawlSpider): name = "huhu"
allowed_domains = ["cnblogs.com"]
start_urls = [
"http://www.cnblogs.com/",
] rules = ( Rule(SgmlLinkExtractor(allow=('sitehome/p/[0-9]+', ),)), Rule(SgmlLinkExtractor(allow=('[^\s]+/p/', )), callback='parse_item'), ) def parse_item(self, response):
global add #用于统计博文的数量 print add
add+=1 sel = Selector(response)
items = [] item = Website()
item['headTitle'] = sel.xpath('/html/head/title/text()').extract()#观察网页对应得html源码
item['url'] = response
print item
items.append(item)
return items

参考:http://scrapy-chs.readthedocs.org/zh_CN/latest/topics/spiders.html

使用scrapy框架爬取自己的博文的更多相关文章

  1. 使用scrapy框架爬取自己的博文(2)

    之前写了一篇用scrapy框架爬取自己博文的博客,后来发现对于中文的处理一直有问题- - 显示的时候 [u'python\u4e0b\u722c\u67d0\u4e2a\u7f51\u9875\u76 ...

  2. 使用scrapy框架爬取自己的博文(3)

    既然如此,何不再抓一抓网页的文字内容呢? 谷歌浏览器有个审查元素的功能,就是按树的结构查看html的组织形式,如图: 这样已经比较明显了,博客的正文内容主要在div 的class = cnblogs_ ...

  3. scrapy框架爬取笔趣阁完整版

    继续上一篇,这一次的爬取了小说内容 pipelines.py import csv class ScrapytestPipeline(object): # 爬虫文件中提取数据的方法每yield一次it ...

  4. scrapy框架爬取笔趣阁

    笔趣阁是很好爬的网站了,这里简单爬取了全部小说链接和每本的全部章节链接,还想爬取章节内容在biquge.py里在加一个爬取循环,在pipelines.py添加保存函数即可 1 创建一个scrapy项目 ...

  5. Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)

    1. Scrapy框架 Scrapy是python下实现爬虫功能的框架,能够将数据解析.数据处理.数据存储合为一体功能的爬虫框架. 2. Scrapy安装 1. 安装依赖包 yum install g ...

  6. 爬虫入门(四)——Scrapy框架入门:使用Scrapy框架爬取全书网小说数据

    为了入门scrapy框架,昨天写了一个爬取静态小说网站的小程序 下面我们尝试爬取全书网中网游动漫类小说的书籍信息. 一.准备阶段 明确一下爬虫页面分析的思路: 对于书籍列表页:我们需要知道打开单本书籍 ...

  7. 基于python的scrapy框架爬取豆瓣电影及其可视化

    1.Scrapy框架介绍 主要介绍,spiders,engine,scheduler,downloader,Item pipeline scrapy常见命令如下: 对应在scrapy文件中有,自己增加 ...

  8. scrapy框架爬取豆瓣读书(1)

    1.scrapy框架 Scrapy,Python开发的一个快速.高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘.监测和自动化测试 ...

  9. scrapy框架爬取糗妹妹网站妹子图分类的所有图片

    爬取所有图片,一个页面的图片建一个文件夹.难点,图片中有不少.gif图片,需要重写下载规则, 创建scrapy项目 scrapy startproject qiumeimei 创建爬虫应用 cd qi ...

随机推荐

  1. 判断回文字符串(c,python)

    回文字符串:一个字符串,不论是从左往右,还是从右往左,字符的顺序都是一样的(如abba,abcba等) 判断回文字符串比较简单,即用两个变量left,right模仿指针(一个指向第一个字符,一个指向最 ...

  2. 有名管道mkfifo

    int mkfifo(const char *pathname, mode_t mode); int mknod(const char *pathname, mode_t mode, dev_t de ...

  3. ShiroFilterFactoryBean 处理拦截资源文件问题(Shiro权限管理)

    一.需要定义ShiroFilterFactoryBean()方法,而ShiroFilterFactoryBean.class是实现了FactoryBean和BeanPostProcessor接口: 1 ...

  4. SVM学习(五):松弛变量与惩罚因子

    https://blog.csdn.net/qll125596718/article/details/6910921 1.松弛变量 现在我们已经把一个本来线性不可分的文本分类问题,通过映射到高维空间而 ...

  5. chrome plugins

    ehpomnigmfglbkmnboidmmhhmicfdmom_1_1_0知行-时间管理 必开 Adkill and Media Download Cnblogs Wz(博客园网摘) Kami - ...

  6. HDU 3697贪心

    额...大意是你可以决定什么时候选课.然后呢.每五分钟只有一次机会选.每种课限制选课时间.问你能选到的课最多有多少. 感觉一点都不水.是自己太菜了吗? #include<stdio.h> ...

  7. Apache自带性能测试工具ab的使用

    Apache服务器套件自带ab,只要安装Apache即可,无需另行安装ab.ab位于%ApacheHome%/bin目录下(“%ApacheHome%”为Aapche安装路径),你也可以把ab.exe ...

  8. L224 词汇题

    Elaborate 精心的 preparations were being made for the Prime Minister’s official visit to the four forei ...

  9. js push ,pop ,concat ,join方法

    push 方法 将新元素添加到一个数组中,并返回数组的新长度值. arrayObj.push([item1 [item2[. . . [itemN ]]]]) 说明 push 方法将以新元素出现的顺序 ...

  10. Git内网服务搭建全过程

    看到一篇搭建git服务器的文章,主要是公司内网搭建的,讲得非常详细,比廖雪峰的要完整,必须赞! http://developer.51cto.com/art/201507/483448.htm