初识scrapy

简介:

b77ad088d86287e00da1a31232ff896e.jpg

scrapy由下面几个部分组成

    spiders:爬虫模块,负责配置需要爬取的数据和爬取规则,以及解析结构化数据

    items:定义我们需要的结构化数据,使用相当于dict 

    pipelines:管道模块,处理spider模块分析好的结构化数据,如保存入库等

    middlewares:中间件,相当于钩子,可以对爬取前后做预处理,如修改请求headerurl过滤等

参考 http://python.gotrained.com/scrapy-tutorial-web-scraping-craigslist/

        https://doc.scrapy.org/en/latest/

本篇文档只写了常见的spiders例子,其余部分(items、pipelines、settings等)请参考后期blog

例1 在同一个页面上抓取内容 (抓取七月在线精品课程的名称、课程信息、开课时间)

1
2
3
4
5
6
7
8
9
10
11
import  scrapy
class  julyClassSpider(scrapy.Spider):
     name = 'julyclass'
     start_urls = [ 'https://www.julyedu.com/category/index' ]
     def  parse( self ,response):
         for  classinfo  in  response.xpath( '//div[@class="item"]/div/div' ):
             classname = classinfo.xpath( 'a[1]/h4/text()' ).extract_first()
             classdate = classinfo.xpath( 'a[1]/p[2]/text()' ).extract_first()
             imageaddr = response.url + classinfo.xpath( 'a[1]/img[1]/@src' ).extract_first()
             #print("classname:%s; classdate:%s; imageaddr: %s " %(classname,classdate,imageaddr))
             yield  { "classname" :classname, "classdate" :classdate, "imageaddr" :imageaddr}

在连续页面上抓取内容(抓取博客园前10页的精华贴)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import  scrapy
import  re
class  cnblogsSpider(scrapy.Spider):
     name = "cnblogs"
     start_urls = [ 'https://www.cnblogs.com/pick/' + str (n) + '/'  for  in  range ( 1 , 10 )]
     def  parse( self ,response):
         for  post  in  response.xpath( '//div[@class="post_item_body"]' ):
             title = post.xpath( 'h3/a/text()' ).extract_first()
             href = post.xpath( 'h3/a/@href' ).extract_first()
             pubdate = post.xpath( 'div[@class="post_item_foot"]/text()' )[ 1 ].extract().strip()
             pubdate = re.split( ' ' ,pubdate)[ 1 ] + ' ' + re.split( ' ' ,pubdate)[ 2 ]
             comments = post.xpath( 'div[@class="post_item_foot"]/span[1]/a/text()' ).extract_first()
             comments = re.split( '\(|\)' ,comments)[ 1 ]
             reads = post.xpath( 'div[@class="post_item_foot"]/span[2]/a/text()' ).extract_first()
             reads = re.split( '\(|\)' ,reads)[ 1 ]
             #print(title,href,pubdate,comments,reads)
             yield  { 'title' :title, 'url' :href, 'pubdate' :pubdate, 'comments' :comments, 'reads' :reads}

运行:scrapy runspider scrapy2.py

urls是通过for拼接而成的list


通过指定按钮(Next)连续抓取多个页面内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import  scrapy
import  re
class  cnblogsSpider(scrapy.Spider):
     name = "cnblogs"
     start_urls = [ 'https://www.cnblogs.com/pick/' ]
     def  parse( self ,response):
         for  post  in  response.xpath( '//div[@class="post_item_body"]' ):
             title = post.xpath( 'h3/a/text()' ).extract_first()
             href = post.xpath( 'h3/a/@href' ).extract_first()
             pubdate = post.xpath( 'div[@class="post_item_foot"]/text()' )[ 1 ].extract().strip()
             pubdate = re.split( ' ' ,pubdate)[ 1 ] + ' ' + re.split( ' ' ,pubdate)[ 2 ]
             comments = post.xpath( 'div[@class="post_item_foot"]/span[1]/a/text()' ).extract_first()
             comments = re.split( '\(|\)' ,comments)[ 1 ]
             reads = post.xpath( 'div[@class="post_item_foot"]/span[2]/a/text()' ).extract_first()
             reads = re.split( '\(|\)' ,reads)[ 1 ]
             #print(title,href,pubdate,comments,reads)
             yield  { 'title' :title, 'url' :href, 'pubdate' :pubdate, 'comments' :comments, 'reads' :reads}
         #print("========="+response.url+"==========")
         url = response.xpath( '//div[@class="pager"]/a[last()]/@href' ).extract()[ 0 ]
         nexturl = response.urljoin(url)
         yield  scrapy.Request(nexturl,callback = self .parse)

通过“Next” 按钮获取下一页的url,然后分析.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import  scrapy
import  re
class  humorSpider(scrapy.Spider):
     name = 'humor'
     start_urls = [ 'http://quotes.toscrape.com/tag/humor/page/1/' ]
     def  parse( self ,response):
         for  humor  in  response.xpath( '//div[@class="quote"]' ):
             sentence = humor.xpath( 'span[1]/text()' ).extract_first()
             author = humor.xpath( 'span[2]/small/text()' ).extract_first()
             yield  { 'sentence' :sentence, 'author' :author}
         next_url = response.xpath( '//ul[@class="pager"]/li/a/@href' ).extract_first()
         pattern = re. compile (r '/' )
         if  next_url  is  not  None  and  pattern.split(next_url)[ - 2 ]>pattern.split(response.url)[ - 2 ]:
             next_url = response.urljoin(next_url)
             #print(next_url)
             yield  scrapy.Request(next_url,callback = self .parse)

4 通过多个函数分析不同页面

scrapy startproject qqnews

1
2
3
4
5
6
7
8
9
10
11
12
13
14
tree
.
|____qqnews
| |______init__.py
| |______pycache__
| |____items.py
| |____middlewares.py
| |____pipelines.py
| |____settings.py
| |____spiders
| | |______init__.py
| | |______pycache__
| | |____qqnews.py
|____scrapy.cfg

cd qqnews/spiders/

cat qqnews.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import  scrapy
class  qqNewsSpider(scrapy.Spider):
     name  =  'qqnews'
     start_urls  =  [ 'http://news.qq.com/' ]
     def  parse( self ,response):
         for  url  in  response.xpath( '//div[@class="text"]/em/a/@href' ).extract():
             yield  scrapy.Request(url,callback = self .parse_news)
     def  parse_news( self ,response):
         try :
             title = response.xpath( '//div[@class="hd"]/h1/text()' ).extract()[ 0 ]
             type = response.xpath( '//div[@class="a_Info"]/span[1]/a/text()' ).extract()[ 0 ]
             source = response.xpath( '//div[@class="a_Info"]/span[2]/a/text()' ).extract()[ 0 ]
             time = response.xpath( '//span[@class="a_time"]/text()' ).extract()[ 0 ]
             print (title, type ,source,time)
         except :
             print ( "exception" )

运行:scrapy crawl qqnews -o news.csv










本文转自 meteor_hy 51CTO博客,原文链接:http://blog.51cto.com/caiyuanji/1982130,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
数据采集 存储 数据可视化
介绍一下常见的爬虫框架或库,如`Scrapy`。
【2月更文挑战第22天】【2月更文挑战第70篇】介绍一下常见的爬虫框架或库,如`Scrapy`。
|
4月前
|
数据采集 中间件 Python
scrapy中使用senlenium
scrapy中使用senlenium
18 0
|
5月前
|
数据采集 存储 数据挖掘
scrapy介绍
scrapy介绍
58 0
|
6月前
|
数据采集 Web App开发 中间件
Scrapy爬虫框架
Scrapy爬虫框架
91 1
Scrapy爬虫框架
|
12月前
|
数据采集 JSON 前端开发
Scrapy 的初步认识
Scrapy 是一个高级的 Python 爬虫框架,它不仅包含了爬虫的特性,还可以方便的将爬虫获取的数据保存到 csv、json 等文件中。 Scrapy 使用了 Twisted 作为框架,Twisted 是事件驱动的,对于会阻塞线程的操作(访问文件、数据库等),比较适合异步的代码。
|
数据采集 存储 中间件
Scrapy基础详解
Scrapy基础详解
212 0
Scrapy基础详解
|
数据采集 中间件 调度
强大的爬虫框架 Scrapy
本节来介绍一个强大的爬虫框架 Scrapy。Scrapy 是一个基于 Twisted 的异步处理框架,是纯 Python 实现的爬虫框架,其架构清晰,模块之间的耦合程度低,可扩展性极强,可以灵活完成各种需求。
100 0
|
数据采集 存储 JSON
scrapy学习
scrapy学习
153 0
|
XML Ubuntu 安全
二、安装 Scrapy
二、安装 Scrapy
178 0
|
数据采集 算法 中间件
scrapy相关专题总结
本月的scrapy已经写完,关于scrapy写了常用的中间件、数据管道、以及scrapy的相关源码,但是感觉可写的内容不是很多,要门是使用相关的要么是一些不用关注的源码。 所以写完scrapy之后还写了些一些其他内容,算是在充数吧,同时预下一个专题是python的数据结构和算法,将对九大算法及其常用数据结构分享,同时将算法应用于数据结构上。
1669 0