Python爬虫,利用scrapy来编写一个爬虫!

《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!

本文将介绍我是如何在python爬虫里面一步一步踩坑,然后慢慢走出来的,期间碰到的所有问题我都会详细说明,让大家以后碰到这些问题时能够快速确定问题的来源,后面的代码只是贴出了核心代码,更详细的代码暂时没有贴出来。

这是小编准备的python爬虫学习资料,加群:696541369即可免费获取!

《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!

流程一览

首先我是想爬某个网站上面的所有文章内容,但是由于之前没有做过爬虫(也不知道到底那个语言最方便),所以这里想到了是用python来做一个爬虫(毕竟人家的名字都带有爬虫的含义:smile:),我这边是打算先将所有从网站上爬下来的数据放到 ElasticSearch 里面, 选择 ElasticSearch 的原因是速度快,里面分词插件,倒排索引,需要数据的时候查询效率会非常好(毕竟爬的东西比较多:smile:),然后我会将所有的数据在 ElasticSearch 的老婆 kibana 里面将数据进行可视化出来,并且分析这些文章内容,可以先看一下预期可视化的效果(上图了),这个效果图是 kibana6.4 系统给予的帮助效果图(就是说你可以弄成这样,我也想弄成这样:grin:)。后面我会发一个dockerfile上来(现在还没弄:flushed:)。

《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!

环境需求

  1. Jdk (Elasticsearch需要)
  2. ElasticSearch (用来存储数据)
  3. Kinaba (用来操作ElasticSearch和数据可视化)
  4. Python (编写爬虫)
  5. Redis (数据排重)

这些东西可以去找相应的教程安装,我这里只有ElasticSearch的安装:cry:点我获取安装教程

第一步,使用python的pip来安装需要的插件(第一个坑在这儿)

  1. tomd :将html转换成markdown

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>pip3 install tomd
复制代码
</pre>

  1. redis :需要python的redis插件

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>pip3 install redis
复制代码
</pre>

  1. scrapy :框架安装(坑)
  2. 首先我是像上面一样执行了

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>pip3 install scrapy
复制代码
</pre>

  1. 然后发现缺少 gcc 组件 error: command ‘gcc’ failed with exit status 1

《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!

  1. 然后我就找啊找,找啊找,最后终于找到了正确的解决方法(期间试了很多错误答案:sob:)。最终的解决办法就是使用 yum 来安装 python34-devel , 这个 python34-devel 根据你自己的python版本来,可能是python-devel,是多少版本就将中间的34改成你的版本, 我的是3.4.6

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>yum install python34-devel
复制代码
</pre>

  1. 安装完成过后使用命令 scrapy 来试试吧。

《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!

第二步,使用scrapy来创建你的项目

  1. 输入命令 scrapy startproject scrapyDemo , 来创建一个爬虫项目

《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!

  1. 使用genspider来生成一个基础的spider,使用命令 scrapy genspider demo juejin.im , 后面这个网址是你要爬的网站,我们先爬自己家的:joy:

《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!

  1. 查看生成的目录结构

《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!

第三步,打开项目,开始编码

  1. 查看生成的的demo.py的内容

《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!

  1. 可以使用第二种方式,将start_urls给提出来

《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!

  1. 编写 parse 方法的代码

《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!

  1. 编写pipeline类,这个pipeline是一个管道,可以将所有yield关键字返回的数据都交给这个管道处理,但是需要在settings里面配置一下pipeline才行

《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!

第四步,运行代码查看效果

  1. 使用 scrapy list 查看本地的所有爬虫

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>liaochengdeMacBook-Pro:scrapyDemo liaocheng《Python爬虫,利用scrapy来编写一个爬虫!》
复制代码
</pre>

  1. 使用 scrapy crawl demo 来运行爬虫

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>scrapy crawl demo
复制代码
</pre>

  1. 到kibana里面看爬到的数据,执行下面的命令可以看到数据

《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!
《Python爬虫,利用scrapy来编写一个爬虫!》 Python爬虫,利用scrapy来编写一个爬虫!

    原文作者:妄心xyx
    原文地址: https://www.jianshu.com/p/e9cba57f8605
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞