windows下使用python的scrapy爬虫框架,爬取个人博客文章内容信息
│ scrapy.cfg
│
└─vpoetblog
│ items.py
│ pipelines.py
│ settings.py
│ __init__.py
│
└─spiders
__init__.py
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
这里我们要新建一些文件,最终的目录结构如下:
│ scrapy.cfg
│ data.txt //用于保存抓取到的数据
└─doubanmoive
│ items.py //用于定义抓取的item
│ pipelines.py //用于将抓取的数据进行保存
│ settings.py
│ __init__.py
│
└─spiders
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
blog_spider.py //主爬虫函数 用于定义抓取规则等
__init__.py
- 1
- 2
- # -*- coding: cp936 -*-
- from scrapy.item import Item, Field
- class VpoetblogItem(Item):
- # define the fields for your item here like:
- # name = Field()
- article_name = Field() #文章名字
- public_time = Field() #发表时间
- read_num = Field() #阅读数量
- # -*- coding: utf-8 -*-
- import sys
- reload(sys)
- sys.setdefaultencoding('utf-8')
- from scrapy.exceptions import DropItem
- from scrapy.conf import settings
- from scrapy import log
- class Pipeline(object):
- def __init__(self):
- print 'abc'
- def process_item(self, item, spider):
- #Remove invalid data
- #valid = True
- #for data in item:
- #if not data:
- #valid = False
- #raise DropItem("Missing %s of blogpost from %s" %(data, item['url']))
- #print 'crawl no data.....\n'
- #if valid:
- #Insert data into txt
- input = open('data.txt', 'a')
- input.write('article_name:'+item['article_name'][0]+' ');
- input.write('public_time:'+item['public_time'][0]+' ');
- input.write('read_num:'+item['read_num'][0]+' ');
- input.close()
- return item
- # Scrapy settings for vpoetblog project
- #
- # For simplicity, this file contains only the most important settings by
- # default. All the other settings are documented here:
- #
- # http://doc.scrapy.org/topics/settings.html
- #
- BOT_NAME = 'vpoetblog'
- BOT_VERSION = '1.0'
- SPIDER_MODULES = ['vpoetblog.spiders']
- NEWSPIDER_MODULE = 'vpoetblog.spiders'
- ITEM_PIPELINES={
- 'vpoetblog.pipelines.Pipeline':300
- }
- DOWNLOAD_DELAY = 2
- RANDOMIZE_DOWNLOAD_DELAY = True
- USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.54 Safari/536.5'
- COOKIES_ENABLED = True
blog_spider.py内容如下:
- # -*- coding: utf-8 -*-
- from scrapy.selector import HtmlXPathSelector
- from scrapy.contrib.spiders import CrawlSpider,Rule
- from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
- from vpoetblog.items import VpoetblogItem
- class MoiveSpider(CrawlSpider):
- name="vpoetblog"
- allowed_domains=["blog.csdn.net"]
- start_urls=["http://blog.csdn.net/u013018721/article/list/1"]
- rules=[
- Rule(SgmlLinkExtractor(allow=(r'http://blog.csdn.net/u013018721/article/list/\d+'))),
- Rule(SgmlLinkExtractor(allow=(r'http://blog.csdn.net/u013018721/article/details/\d+')),callback="parse_item"),
- ]
- def parse_item(self,response):
- sel=HtmlXPathSelector(response)
- item=VpoetblogItem()
- item['article_name']=sel.select('//*[@class="link_title"]/a/text()').extract()
- item['public_time']=sel.select('//*[@class="link_postdate"]/text()').extract()
- item['read_num']=sel.select('//*[@class="link_view"]/text()').extract()
- return item
windows下使用python的scrapy爬虫框架,爬取个人博客文章内容信息的更多相关文章
- 手把手教大家如何用scrapy爬虫框架爬取王者荣耀官网英雄资料
之前被两个关系很好的朋友拉入了王者荣耀的大坑,奈何技术太差,就想着做一个英雄的随查手册,这样就可以边打边查了.菜归菜,至少得说明咱打王者的态度是没得说的,对吧?大神不喜勿喷!!!感谢!!废话不多说,开 ...
- Python爬取CSDN博客文章
0 url :http://blog.csdn.net/youyou1543724847/article/details/52818339Redis一点基础的东西目录 1.基础底层数据结构 2.win ...
- scrapy爬虫框架爬取招聘网站
目录结构 BossFace.py文件中代码: # -*- coding: utf-8 -*-import scrapyfrom ..items import BossfaceItemimport js ...
- python 爬虫 爬取序列博客文章列表
python中写个爬虫真是太简单了 import urllib.request from pyquery import PyQuery as PQ # 根据URL获取内容并解码为UTF-8 def g ...
- Python之Scrapy爬虫框架安装及简单使用
题记:早已听闻python爬虫框架的大名.近些天学习了下其中的Scrapy爬虫框架,将自己理解的跟大家分享.有表述不当之处,望大神们斧正. 一.初窥Scrapy Scrapy是一个为了爬取网站数据,提 ...
- 【python】Scrapy爬虫框架入门
说明: 本文主要学习Scrapy框架入门,介绍如何使用Scrapy框架爬取页面信息. 项目案例:爬取腾讯招聘页面 https://hr.tencent.com/position.php?&st ...
- 【网络爬虫】【python】网络爬虫(五):scrapy爬虫初探——爬取网页及选择器
在上一篇文章的末尾,我们创建了一个scrapy框架的爬虫项目test,现在来运行下一个简单的爬虫,看看scrapy爬取的过程是怎样的. 一.爬虫类编写(spider.py) from scrapy.s ...
- 【Python】【爬虫】爬取酷狗TOP500
好啦好啦,那我们来拉开我们的爬虫之旅吧~~~ 这一只小爬虫是爬取酷狗TOP500的,使用的爬取手法简单粗暴,目的是帮大家初步窥探爬虫长啥样,后期会慢慢变得健壮起来的. 环境配置 在此之前需要下载一个谷 ...
- Webmagic 爬虫框架 爬取马蜂窝、携程旅游、汽车之家游记信息
WebMagic学习 遇到的问题 Log4j错误 解决:在src目录下添加配置文件 log4j.properties log4j.rootLogger=INFO, stdout, file log4j ...
随机推荐
- SQLAlchemy中scoped_session实现线程安全
不多说,先上代码 from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine from sqlalchem ...
- Python开发【Django】:基础
Django基本配置 Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Se ...
- kubernetes实战(八):k8s集群安全机制RBAC
1.基本概念 RBAC(Role-Based Access Control,基于角色的访问控制)在k8s v1.5中引入,在v1.6版本时升级为Beta版本,并成为kubeadm安装方式下的默认选项, ...
- Android仿今日头条手界面
public class MyIndicator extends HorizontalScrollView implements ViewPager.OnPageChangeListener { pr ...
- java-基础-【二】内部类与静态内部类
一.说明 java允许我们在一个类里面定义静态类.比如内部类(nested class).把nested class封闭起来的类叫外部类.在java中,我们不能用static修饰顶级类(top lev ...
- django基于cors做跨域处理
背景知识:跨域相关与cors策略 1.安装django-cors-headers pip install django-cors-headers 2.settings.py配置 INSTALLED_A ...
- 分布式版本管理git学习资料整理推荐
一.什么是git? Git is a free and open source distributed version control system designed to handle everyt ...
- 2.4 The Object Model -- Computed Properties and Aggregate Data with @each(计算的属性和使用@each聚合数据)
1. 通常,你可能有一个计算的属性依赖于数组中的所有元素来确定它的值.例如,你可能想要计算controller中所有todo items的数量,以此来确定完成了多少任务. export default ...
- arguments参数对象
//求任意个数的和 function test() { var sum = 0; for (var i = 0; i < arguments.length; i++) { sum += argu ...
- http之http1.0和http1.1的区别
下面主要从几个不同的方面介绍HTTP/1.0与HTTP/1.1之间的差别,当然,更多的内容是放在解释这种差异背后的机制上. 1 可扩展性 可扩展性的一个重要原则:如果HTTP的某个实现接收到了自身未定 ...