出处:http://blog.chinaunix.net/uid-23500957-id-3788157.html
1、创建项目
scrapy startproject fjsen
2、定义items--items.py
# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/topics/items.html
from scrapy.item import Item, Field
class FjsenItem(Item):
# define the fields for your item here like:
# name = Field()
title=Field()#文章标题
link=Field()#文章链接
addtime=Field()#文章时间
3、编写爬虫
新建一个fjsen_spider.py,内容如下:
#-*- coding: utf-8 -*-
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from fjsen.items import FjsenItem
class FjsenSpider(BaseSpider):
name="fjsen"
allowed_domains=["fjsen.com"]
start_urls=['http://www.fjsen.com/j/node_94962_'+str(x)+'.htm' for x in range(2,11)]+['http://www.fjsen.com/j/node_94962.htm']
def parse(self,response):
hxs=HtmlXPathSelector(response)
sites=hxs.select('//ul/li')
items=[]
for site in sites:
item=FjsenItem()
item['title']=site.select('a/text()').extract()
item['link'] = site.select('a/@href').extract()
item['addtime']=site.select('span/text()').extract()
items.append(item)
return items
4、入库---在pipelines.py中处理
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/topics/item-pipeline.html import sqlite3
from os import path
from scrapy import signals
from scrapy.xlib.pydispatch import dispatcher
class FjsenPipeline(object):
def __init__(self):
self.conn=None
dispatcher.connect(self.initialize,signals.engine_started)
dispatcher.connect(self.finalize,signals.engine_stopped)
def process_item(self,item,spider):
self.conn.execute('insert into fjsen values(?,?,?,?)',(None,item['title'][0],'http://www.fjsen.com/'+item['link'][0],item['addtime'][0]))
return item
def initialize(self):
if path.exists(self.filename):
self.conn=sqlite3.connect(self.filename)
else:
self.conn=self.create_table(self.filename)
def finalize(self):
if self.conn is not None:
self.conn.commit()
self.conn.close()
self.conn=None
def create_table(self,filename):
conn=sqlite3.connect(filename)
conn.execute("""create table fjsen(id integer primary key autoincrement,title text,link text,addtime text)""")
conn.commit()
return conn
5、修改配置--setting.py
ITEM_PIPELINES=['fjsen.pipelines.FjsenPipeline']
6、执行
scrapy crawl fjsen
==生成一个data.sqlite的数据库文件
相关推荐
scrapy ---爬取豌豆荚并分析数据,通过scrapy框架获取豌豆荚数据储存在mongodb中,并通过pyecharts 分析结果得到图像分析
python库。 资源全名:nimbus_scrapy-3.1.4-py2.py3-none-any.whl
Scrapy框架需要安装的库 Scrapy-1.6.0-py2.py3-none-any.whl 和Twisted-18.9.0-cp37-cp37m-win_amd64.whl
scrapy_redis-0.6.8-py2.py3-none-any.whl 使用方式 pip install xx.whl 即可
Scrapy-2.3.0-py2.py3-none-any.whl 安装Scrapy所需要的资源,安装命令:pip target 本地资源路径
Scrapy-1.5.0-py2.py3-none-any.whl可以用,放心xiasssasa
Scrapy爬虫项目-爬取图片
scrapy-random-useragent, Scrapy中间件为每个请求设置一个随机的User Agent 随机 USER-AGENT由于你使用默认的USER-AGENT 或者一般的,你的nautilus蜘蛛会被服务器识别和阻塞?使用这里 random_useragent 模块并为每...
Python常用库,官方原版whl文件,文件下载到本地后, 直接终端 pip install xxx.whl 安装 scrapy需要twisted库支持,先要安装该库
scrapy-redis分布式爬虫框架+示例
资源分类:Python库 所属语言:Python 资源全名:nimbus_scrapy-3.5.5-py2.py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
这是一个简单的scrapy入门案例,scrapy是1.5python是3
博文代码附件。详情请访问博文《Eclipse集成Scrapy配置调试》 http://blog.csdn.net/otengyue/article/details/48065841
python3+Scrapy爬虫实战(三) —— 使用代理IP,爬取“去哪儿”景点信息 地址址:https://blog.csdn.net/finn_wft/article/details/81112590
基于scrapy编写的爬虫,能够爬取城市二手房的各种信息,比如房价、面积、所处位置等十分方便易用,并采用Beautifulsoup进行页面解析无视反爬机制
运行爬虫scrapy crawl foodSpider,代码已在Linux平台测试,附带2017年12月9日测试结果
Python常用库,官方原版whl文件,文件下载到本地后, 直接终端 pip install xxx.whl 安装
docs-scrapy-org-en-latest.pdf
Scrapy-1.4.0-py2.py3-none-any.whl python3 or python 3.5 Scrapy-1.4.0-py2.py3-none-any.whl python3 or python 3.5 Scrapy-1.4.0-py2.py3-none-any.whl python3 or python 3.5