上回我们说到
python爬虫29 | 使用scrapy爬取糗事百科的例子,告诉你它有多厉害!
WOW!!
scrapy
awesome!!
怎么会有这么牛逼的框架
wow!!
awesome!!
![v2-dc2f18b5457ed4abdfe30d7bf0ee2ab8_b.gif](http://img-03.proxy.5ce.com/view/image?&type=2&guid=57c5abdd-962e-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-dc2f18b5457ed4abdfe30d7bf0ee2ab8_b.gif)
用 scrapy 来爬取数据
岂!不!是!非!常!爽!
wow!!
接下来就是我独享的moment
哦不
接下来就是
学习 python 的正确姿势
![v2-a87a81f6db606d616c08e7e7943582d6_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=57c5abdd-962e-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-a87a81f6db606d616c08e7e7943582d6_b.jpg)
我们已经创建了爬取糗事百科的项目
并且把糗事百科的前两页的作者和段子爬取到 json 文件了
![v2-24abe5af641b93256408822ba63f0a8e_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=57c5abdd-962e-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-24abe5af641b93256408822ba63f0a8e_b.jpg)
这次
我们将我们要爬取所有的数据
使用 scrapy 存储到 mangodb 中
在此之前还是先介绍一下我们使用 scrapy 创建出来的文件目录
各个文件代表的都是啥意思
免得又有些 b 友当场懵逼
![v2-8e0a0f7eb0f73501f2cee491f684701e_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=57c5abdd-962e-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-8e0a0f7eb0f73501f2cee491f684701e_b.jpg)
我们从上往下依个介绍一下
![v2-cc94ff411a8af970e7fc9c354e024547_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=57c5abdd-962e-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-cc94ff411a8af970e7fc9c354e024547_b.jpg)
这个 spiders 目录呢
就是用来存放我们写爬虫文件的地方
items.py
就是用来定义我们要存储数据的字段
middlewares.py
就是中间件,在这里面可以做一些在爬虫过程中想干的事情,比如爬虫在响应的时候你可以做一些操作
pipelines.py
这是我们用来定义一些存储信息的文件,比如我们要连接 MySQL或者 MongoDB 就可以在这里定义
settings.py
这个文件用来定义我们的各种配置,比如配置请求头信息等
以上就是 scrapy 生成的目录中主要文件的作用
![v2-a0c579ddb3ce9b288ffdebaf65ee8e22_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=57c5abdd-962e-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-a0c579ddb3ce9b288ffdebaf65ee8e22_b.jpg)
ok
接下来我们就进入代码中
我们上次创建了 QiushiSpider 来写我们的爬虫
当时我们只是获取了前两页的数据
我们要获取所有页面的数据怎么玩呢
打开糗事百科的链接可以看到
![v2-f197f843c2476192cdef8b423309b1cc_b.png](http://img-03.proxy.5ce.com/view/image?&type=2&guid=57c5abdd-962e-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-f197f843c2476192cdef8b423309b1cc_b.png)
13 页的数据
其实按照以前我们直接写个 for 循环就可以了
不过我们这次还可以使用 scrapy 的 follow 函数
具体使用是这样的
我们先获取下一页的链接
![v2-320a962892cbed3358d4e849ddf7dc0f_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=57c5abdd-962e-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-320a962892cbed3358d4e849ddf7dc0f_b.jpg)
由于下一页这个按钮都是在最后一个 li 标签中的
所以用 xpath 获取就这样
next_page = response.xpath('//*[@id="content-left"]/ul/li[last()]/a').attrib['href']
接着我们就可以让它去请求下一页的内容数据了
if next_page is not None:
yield response.follow(next_page, callback=self.parse)
你也可以用 urljoin 的方式
# if next_page is not None:
# next_page = response.urljoin(next_page)
# yield scrapy.Request(next_page, callback=self.parse)
这样我们就可以获取到所有页面的数据了
接下来我们要把所有的数据保存到数据库
首先我们在 items.py 中定义一下我们要存储的字段
import scrapy
class QiushibaikeItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
author = scrapy.Field()
content = scrapy.Field()
_id = scrapy.Field()
接着我们在 parse 方法中将获取到的数据赋值给 item
具体来说就是这样
def parse(self, response):
content_left_div = response.xpath('//*[@id="content-left"]')
content_list_div = content_left_div.xpath('./div')
for content_div in content_list_div:
item = QiushibaikeItem()
item['author'] = content_div.xpath('./div/a[2]/h2/text()').get()
item['content'] = content_div.xpath('./a/div/span/text()').getall()
item['_id'] = content_div.attrib['id']
yield item
next_page = response.xpath('//*[@id="content-left"]/ul/li[last()]/a').attrib['href']
if next_page is not None:
yield response.follow(next_page, callback=self.parse)
第 7 行就是获取我们刚刚定义的 item 的类
8-10 行就是相应的赋值
那么我们定义好了要存储的字段以及写好了数据爬取
接下来还有一步
就是定义好我们要存储的数据库
到 pipelines.py 中
class QiushibaikePipeline(object):
def __init__(self):
self.connection = pymongo.MongoClient('localhost', 27017)
self.db = self.connection.scrapy
self.collection = self.db.qiushibaike
def process_item(self, item, spider):
if not self.connection or not item:
return
self.collection.save(item)
def __del__(self):
if self.connection:
self.connection.close()
在这里我们连接到本地的 MongoDB
建立了 scrapy 数据库及以下的 qiushibaike
接下来还要在 settings.py 文件中配置下
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'qiushibaike.pipelines.QiushibaikePipeline': 300,
}
这样才可以使用到pipelines
当然我们还可以在 settings.py 里面做更多的设置
比如设置请求头
# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/73.0.3683.86 Chrome/73.0.3683.86 Safari/537.36'
ok
搞定了之后
我们使用命令来抓取一下
scrapy crawl qiushibaike
运行之后
我们打开 MongoDB 看看
![v2-162bf453c63864d7475018953d80a384_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=57c5abdd-962e-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-162bf453c63864d7475018953d80a384_b.jpg)
可以看到
所有的数据就被我爬取到 MongoDB 了
![v2-44db59d5be67320feed795113a11a3b4_b.gif](http://img-01.proxy.5ce.com/view/image?&type=2&guid=57c5abdd-962e-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-44db59d5be67320feed795113a11a3b4_b.gif)
ok
以上就是 scrapy 的简单又牛逼的操作
更多 scrapy 的牛逼操作可以前往 https://doc.scrapy.org 了解
这两篇所涉及的源代码已经上传
可以在公众号后台发送 scrapy 获取
那么
我们下回见
peace
![v2-d3009014e01b518c4c87faea3e506256_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=57c5abdd-962e-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-d3009014e01b518c4c87faea3e506256_b.jpg)
对了
有个事
你希望接下来这个公众号能有更多什么教程
例如
爬虫实战?
ubuntu?
vim?
...?
请扫下方的码评论告诉我一下
![v2-8173e4237565b03e33a99bbbb2feb2dc_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=57c5abdd-962e-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-8173e4237565b03e33a99bbbb2feb2dc_b.jpg)
![v2-9014cb62502f793e4c21b64f8f5d8301_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=57c5abdd-962e-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-9014cb62502f793e4c21b64f8f5d8301_b.jpg)