用Python爬取WordPress官网所有插件 !

前言

只要是用WordPress的人或多或少都会装几个插件,可以用来丰富扩展WordPress的各种功能。围绕WordPress平台的插件和主题已经建立了一个独特的经济生态圈和开发者社区,养活了众多的WordPress相关的开发公司和开发者。各种强大的WordPress插件也层出不穷,有的甚至可以做出功能完善的网站,比如招聘网站、分类信息网站、电商网站、点评网站、培训网站等等,令我赞叹不已。

最近一直沉迷于研究 WordPress,仿佛事隔多年与初恋情人再续前缘一般陷入热恋。这几天突发奇想能不能把WordPress上这么多眼花缭乱的插件都爬下来,看看能不能分析出一点有意思的东西来。

总体思路

官网 https://wordpress.org/plugins/ 上列出了一共有 54,520 个插件。记得以前在官网上可以按各种分类浏览的,现在只有推荐的插件、收藏的插件、流行的插件几大类显示出来,其他的只能靠搜索了。

那么首先第一步我们要知道取哪里可以找到所有的WordPress插件列表,搜了一圈发现WordPress的svn上有这个完整的列表, http://plugins.svn.wordpress.org/ (这个网页比较大,5M多),比官网上的还要齐全,一共7万多个。有了完整列表就好办了。

接下来就是要获取的是插件的各种信息,比如作者、下载量、评分等等。这个可以去哪里获取呢?当然最傻的办法就是根据上面列表中的插件地址,把每个插件的网页down下来再提取,这也就是爬虫干的事。不过 WordPress.org 网站自身的 WordPress.org API 已经给开发者提供了非常方便强大的接口,可以获取到几乎所有 wordprss.org 上的主题、插件、新闻等相关的信息,也支持各种参数和查询。注意,这个和WordPress的REST API是两回事。基本上你可以理解成 Apple.com 的 API 和 iOS 的 API 之间的区别(虽然apple.com并没有什么API。。。)

比如本次需要插件的一些数据,那就可以使用关于插件描述的 API, https://api.wordpress.org/plugins/info/1.0/{slug}.json,slug也就是每个插件唯一的地址,这个在刚才svn上已经可以获取到了。用这个 API 可以返回关于插件的 json 格式的信息,很全面,如下:

<tt-image data-tteditor-tag=”tteditorTag” contenteditable=”false” class=”syl1555315208947″ data-render-status=”finished” data-syl-blot=”image” style=”box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, “WenQuanYi Micro Hei”, “Helvetica Neue”, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;”>
《用Python爬取WordPress官网所有插件 !》 image

<input class=”pgc-img-caption-ipt” placeholder=”图片描述(最多50字)” value=”” style=”box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;”></tt-image>

如果你在学习Python的过程当中有遇见任何问题,可以加入我的python交流学习qq群:683380553,多多交流问题,互帮互助,群里有不错的学习教程和开发工具。学习python有任何问题(学习方法,学习效率,如何就业),可以随时来咨询我,如果你准备学习大数据,也欢迎加入大数据学习交流qq群683380553,每天与大家分享学习资源哦。

有了列表,有了返回格式,接下来就是要把这些信息给扒下来,其实就是重复遍历一遍就可以了,要么用著名 Python 的 Requests库 循环一圈,要么使用 Python 的爬虫框架 Scrapy, 都是可以的,本次决定使用 Scrapy。在存储爬取数据存储方面,本来打算是存入 mongodb 的,但是遇到的一个坑是API返回的json对象里version有的key是带小数点的,比如”0.1″这种是无法直接存入mongodb的,会报错说key不能包括., 所以这可以祭出另外一个厉害的python库 jsonline了, 它可以以jsonl文件的形式一行存储一条json,读写速度也很快。最后爬完所有数据的这个文件有341M之大。。。

最后,有了数据就可以做一些有意思的数据分析了,这一步主要会用到的就是一些常见的 Python 的数据分析工具和图表工具,pandas、numpy、seaborn等。根据上面的返回信息可以看出,能够分析的维度也是很多的,比如哪些作者开发的插件最多、哪些插件的下载量最多、哪些类别的插件最多、哪些国家的开发者最多、每年的插件增长量等等,甚至更进一步可以把所有插件的zip文件下载下来用AI做一些深入的代码分析等等,想想还是挺有意思的,本文的目标也就是提供一种思路和方法,希望能抛砖引玉。

下面进开始进入代码的世界吧

爬取数据

准备工作

要爬数据一般第一步是要确认爬虫的入口网页,也就是从哪里开始爬,沿着入口网页找到下一个URL,找-爬-找,不断循环重复直到结束。一般来说入口网页的分析都可以在scrapy内部进行处理,如果事先就已经可以明确知道所有要请求的网页地址,那么也可以直接把url列表扔进scrpay里,让它顺着列表一直爬爬爬就行了。

本次为了说的清晰一点,爬虫部分不用再次解释,所以分步进行,先把要爬的所有url准备好等下可以直接使用。之前说过了,WordPress所有的插件名称列表在这里可以找到 http://plugins.svn.wordpress.org/ ,这网页是一个非常简单的静态网页,就是一个巨大的ul列表,每一个li就是一个插件名字:

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”> <ul>
<li><a href=”0-delay-late-caching-for-feeds/”>0-delay-late-caching-for-feeds/</a></li>
<li><a href=”0-errors/”>0-errors/</a></li>
<li><a href=”001-prime-strategy-translate-accelerator/”>001-prime-strategy-translate-accelerator/</a></li>
<li><a href=”002-ps-custom-post-type/”>002-ps-custom-post-type/</a></li>
<li><a href=”011-ps-custom-taxonomy/”>011-ps-custom-taxonomy/</a></li>
<li><a href=”012-ps-multi-languages/”>012-ps-multi-languages/</a></li>
</pre>

这里的href就是插件的slug,是wordpress.org用来确定插件的唯一标示。解析这种html对Python来说简直是小菜一碟,比如最常用的 BeautifulSoup 或者 lxmp,这次决定尝试一个比较新的库,Requests-HTML: HTML Parsing for Humans ,这也是开发出Requests库的大神kennethreitz的又一力作,用于解析 HTML 文档的简直不要太爽了。

slug得到后,按照API的url格式地址组合起来,全部写入一个文件中就可以了。

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>from requests_html import HTMLSession
session = HTMLSession()
r = session.get(‘http://plugins.svn.wordpress.org/’)
links = r.html.links
slugs=[ link.replace(‘/’,”) for link in links ]
plugins_urls=[ “https://api.wordpress.org/plugins/info/1.0/{}.json”.format(slug) for slug in slugs ]
with open(‘all_plugins_urls.txt’,’a’) as out:
out.write(“\n”.join(plugins_urls))
</pre>

作为对比,可以看下用 BeautifulSoup 的方法:

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import requests
from bs4 import BeautifulSoup
html=requests.get(“http://plugins.svn.wordpress.org/“).text
soup=BeautifulSoup(html,features=”lxml”)
lis=soup.find_all(‘li’)
baseurl=”https://api.wordpress.org/plugins/info/1.0/
with open(‘all_plugins_urls.txt’,’a’) as out:
for a in soup.find_all(‘a’, href=True):
out.write( baseurl + a[‘href’].replace(‘/’,”) + “.json”+”\n”)
</pre>

就这么一个简单对比还是比较明显的,简单明了。最终,这一步的输出结果就是这个all_plugins_urls.txt文件了,总共有79223个插件。

有了这个列表,其实下面的Scrapy步骤其实完全可以不用,直接拿wget都可以全部简单粗暴的怼下来7万个json文件:

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>wget -i all_plugins_urls.txt
</pre>

或者用requests简单的遍历请求一下就完事了,就可以得到所有插件的数据,进而可以直接进入数据分析阶段了。为了作为演示吧,也算作是一个简单的scrapy的介绍,对于没有接触过scrapy的朋友来说,可以是一个很初步的入门介绍。

安装 scrapy

这一步最简单的方式就是pip安装

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>pip install Scrapy
scarpy -V # 验证一下
</pre>

新建项目 (Project):新建一个新的爬虫项目

scrapy 提供了完善的命令工具可以方便的进行各种爬虫相关的操作。一般来说,使用 scrapy 的第一件事就是创建你的Scrapy项目。我的习惯是首先新建一个文件夹(用要爬的网站来命名,这样可以方便的区分不同网站的爬虫项目)作为总的工作区, 然后进入这个文件夹里新建一个 scrapy 的项目,项目的名字叫做 scrap_wp_plugins,可以改成你想要的名字

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>mkdir ~/workplace/wordpress.org-spider
cd ~/workplace/wordpress.org-spider
scrapy startproject scrap_wp_plugins
</pre>

这样就会自动创建好类似如下的文件结构:

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>├── scrap_wp_plugins
│ ├── init.py
│ ├── pycache
│ ├── items.py
│ ├── middlewares.py
│ ├── pipelines.py
│ ├── settings.py
│ └── spiders
│ ├── init.py
│ └── pycache
└── scrapy.cfg
4 directories, 7 files
</pre>

对我们这个需求来说,除了settings.py需要做一点点修改,其余文件都先不用管它,在这个简单的不能再简单的项目里都用不到。

目前只是一个空架子,啥也干不了,因为还没有爬虫文件,你可以完全纯手写,也可以用模板来生成一个。我们就用scrapy的命令行自动生成一个爬虫,语法格式是这样:

Syntax: scrapy genspider [-t template] <name> <domain>

template 是要使用的爬虫的模板,默认的就是用最基本的一个。

name 就是爬虫的名字,这个可以随便取,等下要开始爬的时候会用到这个名字。好比给你的小蜘蛛取名叫“春十三”,那么在召唤它的时候你就可以大喊一声:“上吧!我的春十三!”

domain 是爬虫运行时允许的域名,好比说:“上吧!我的春十三!只沿着这条路线上!”

所以执行如下命令即可:

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>cd scrap_wp_plugins
scrapy genspider plugins_spider wordpress.org
</pre>

这样就会在spiders文件夹下生出一个叫plugins_spider.py的爬虫文件,也就是在这里面可以填充一些爬取逻辑和内容解析。

制作爬虫(Spider):制作爬虫开始爬取网页

首先我们打开scrap_wp_plugins/plugins_spider.py看下里面的内容:

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”># –– coding: utf-8 –
import scrapy
class PluginsSpiderSpider(scrapy.Spider):
name = ‘plugins_spider’
allowed_domains = [‘wordpress.org’]
start_urls = [‘http://wordpress.org/’]
def parse(self, response):
pass
</pre>

可以看出这就是一个最简单scrapy的Spider的类而已,自动填入了上一步用来创建爬虫时的一些参数。

name:爬虫的识别名,它必须是唯一的,在不同的爬虫中你必须定义不同的名字,就是上一步的命令行里写的plugins_spider.

start_urls:爬虫开始爬的一个URL列表。爬虫从这里开始抓取数据,所以,第一次下载的数据将会从这些URLS开始。其他URL将会从这些起始URL中继承性生成。具体来说,在准备工作那一部分,我们已经得到了一个urls的列表文件all_plugins_urls.txt,现在只需要把这个文件读取进来就好了。

parse():爬虫的方法,调用时候传入从每一个URL传回的Response对象作为参数,response将会是parse方法的唯一的一个参数,

这个方法负责解析返回的数据、匹配抓取的数据(解析为item)并跟踪更多的URL。在本项目中,因为返回的是json,不需要解析任何html,这里为了省事我就直接把json整个存储起来已备后面数据分析的时候再选择需要的字段,当然你也可以根据需要选择过滤掉不需要的json字段。

所以,我们的第一个爬虫就呼之欲出了!请看代码,麻雀虽小五脏俱全

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”># –– coding: utf-8 –
import scrapy
import json
import jsonlines
</pre>

运行爬虫

改完上面的爬虫代码,现在就可以让爬虫跑起来了,“上吧!比卡丘!”

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; 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 plugins_spider
</pre>

哦嚯。。。

<tt-image data-tteditor-tag=”tteditorTag” contenteditable=”false” class=”syl1555315209014″ data-render-status=”finished” data-syl-blot=”image” style=”box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, “WenQuanYi Micro Hei”, “Helvetica Neue”, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;”>
《用Python爬取WordPress官网所有插件 !》 image

<input class=”pgc-img-caption-ipt” placeholder=”图片描述(最多50字)” value=”” style=”box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;”></tt-image>

kv7Xgs.png

<bi style=”box-sizing: border-box; display: block;”>Forbidden by robots.txt</bi>

意外发生了。。。啥也没爬下来??自信看下报错信息,原来是 https://api.wordpress.org/robots.txt 规定了不允许爬虫,而scrapy默认设置里是遵守robot协议的,所以简单绕过就行了,打开 setttings.py, 找到下面这行,把True改为False,意思是:“爱咋咋地,我不理你的robots.txt ”

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”># Obey robots.txt rules
ROBOTSTXT_OBEY = True
</pre>

现在就可以愉快的爬取了。还有一点温馨提示,如果爬取数量比较多,不想中途因为断网或者其他什么鬼知道的意外中断,导致下次又要重新来过,可以加上scrapy的执行日志来保存爬虫状态,下次就会从中断处开始继续爬取

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; 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 plugins_spider -s JOBDIR=spiderlog –logfile log.out &
</pre>

这样就可以安心的去睡个觉,一早起来就能看到热呼呼新鲜出路的WordPress所有的插件信息了。

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