White school Python reptiles (15): urllib basis using (E)

Life is short, I used Python

The foregoing Portal:

White school Python Reptile (1): Opening

White Python crawler Science (2): Pre-preparation (a) is mounted substantially libraries

Getting Started with Linux pre-prepared base (B): white reptile learn Python (3)

Docker basis of pre-entry preparation (III): white reptile learn Python (4)

White school Python Reptile (5): pre-prepared (four) database infrastructure

White school Python Reptile (6): pre-prepared (E) crawler frame installation

White school Python reptiles (7): HTTP basic

White school Python reptiles (8): page basis

White school Python reptiles (9): Reptile basis

White school Python reptiles (10): Session and Cookies

White school Python reptiles (11): urllib Basics (a)

White school Python reptiles (12): urllib Basics (b)

White school Python reptiles (13): urllib Basics (c)

White school Python reptiles (14): urllib based on the use (d)

introduction

Based on the introduction of several front urllib, introduced a module for processing the URL urllib, finally left without making a robotparser module introduction, this article simply to talk robotparser.

Robotparser From the naming point of view, as if to say robots, robot actually here refers to the reptiles.

Before saying Robotparser, we introduce a concept: Robots agreement.

Robots agreement

Robots protocol agreement also called crawlers, robots protocol, its full name for the web crawler exclusion criteria (Robots Exclusion Protocol).

Robots are usually kept in a protocol called a robots.txttext file, the file is usually located in the root directory of your site.

This document describes which directories allow crawling of the site, which does not allow directory crawling.

Although this document is not mandatory in effect, but you can follow the best students This file defines the crawling rules.

最近很多搞爬虫的公司都进去了,各位同学一定引以为戒,做一位知法守法的好公民。

我们来看一下淘宝的 Robots 协议,一起了解下 Robots 协议的语法规则:

以下 robots.txt 内容来源:https://www.taobao.com/robots.txt ,由于内容过多,仅截取部分内容。

User-agent:  Baiduspider
Allow:  /article
Allow:  /oshtml
Allow:  /ershou
Allow: /$
Disallow:  /product/
Disallow:  /

User-Agent:  Googlebot
Allow:  /article
Allow:  /oshtml
Allow:  /product
Allow:  /spu
Allow:  /dianpu
Allow:  /oversea
Allow:  /list
Allow:  /ershou
Allow: /$
Disallow:  /

......

可以看到,一个 robots.txt 总共分为三个部分:

  • User-Agent:描述了搜索爬虫的名称,如果设置为 * 则代表对所有爬虫生效。
  • Allow:指定了云讯爬取的目录。
  • Disallow:指定了不允许爬取的目录,一般和 Allow 配合使用。

比如上面的这个 Robots 协议,其中的内容定义了百度爬虫和谷歌爬虫的爬取规则,其中对百度爬虫定义了的可爬取路径有 /article/oshtml/ershou/$ ,不可爬取的路径有 /product// 目录。

各位同学可能会有疑问,爬虫的名字是哪来的呢?

emmmmmmmmm,这个小编也不清楚,就当做一些固定用法吧,下表列出一些常见的爬虫名称:

爬虫名称 来源 来源网站
BaiduSpider 百度搜索 www.baidu.com
Googlebot 谷歌搜索 www.google.com
360Spider 360 搜索 www.so.com
Bingbot 必应搜索 cn.bing.com
Yisouspider 神马搜索 m.sm.cn
Sogouspider 搜狗搜索 www.sogou.com
Yahoo! Slurp 雅虎搜索 www.yahoo.com
Sosospider 搜搜 www.soso.com

robotparser

官方文档:https://docs.python.org/zh-cn/3.7/library/urllib.robotparser.html

在了解了什么是 Robots 协议之后,我们可以使用 robotparser 来解析 Robots 协议。

该模块提供了一个类 RobotFileParser 它可以根据某网站的 robots.txt 文件来判断一个爬取爬虫是否有权限来爬取这个网站的某个路径。

首先我们看一下这个类的声明:

urllib.robotparser.RobotFileParser(url='')

看起来很简单,只需要在构造方法中传入 robots.txt 的路径即可。

接下来我们来看下这个类所具有的方法:

  • set_url(url): 如果在声明 RobotFileParser 的时候没有传入 robots.txt 的路径,可以调用这个方法传入 robots.txt 的路径。
  • read():读取 robots.txt 文件并进行分析。注意,这个方法执行一个读取和分析操作,如果不调用这个方法,接下来的判断都会为 False ,所以一定记得调用这个方法。这个方法不会返回任何内容,但是执行了读取操作。
  • parse(lines):用来解析 robots.txt 文件,传入的参数是 robots.txt 某些行的内容,它会按照 robots.txt 的语法规则来分析这些内容。
  • can_fetch(useragent, url):该方法传入两个参数,第一个是 User-agent ,第二个是要抓取的 URL 。返回的内容是该搜索引擎是否可以抓取这个 URL ,返回结果是 True 或 False 。
  • mtime():返回的是上次抓取和分析 robots.txt 的时间,这对于长时间分析和抓取的搜索爬虫是很有必要的,你可能需要定期检查来抓取最新的 robots.txt
  • modified():它同样对长时间分析和抓取的搜索爬虫很有帮助,将当前时间设置为上次抓取和分析 robots.txt 的时间。
  • crawl_delay(useragent):从 robots.txt 返回有关用户代理的抓取延迟参数的值。 如果没有这样的参数,或者该参数不适用于指定的用户代理,或者该参数的 robots.txt 条目的语法无效,则返回 None 。
  • request_rate(useragent):以指定的元组 RequestRate(requests,seconds) 的形式从 robots.txt 返回 Request-rate 参数的内容。 如果没有这样的参数,或者该参数不适用于指定的用户代理,或者该参数的 robots.txt 条目的语法无效,则返回 None 。

举一个简单的例子:

import urllib.robotparser

rp = urllib.robotparser.RobotFileParser()
rp.set_url("https://www.taobao.com/robots.txt")
rp.read()

print(rp.can_fetch('Googlebot', 'https://www.taobao.com/article'))
print(rp.can_fetch('Googlebot', "https://s.taobao.com/search?initiative_id=tbindexz_20170306&ie=utf8&spm=a21bo.2017.201856-taobao-item.2&sourceId=tb.index&search_type=item&ssid=s5-e&commend=all&imgfile=&q=iphone&suggest=history_1&_input_charset=utf-8&wq=&suggest_query=&source=suggest"))

执行结果如下:

True
False

小编这里就用某宝做栗子了,首先创建 RobotFileParser 对象,然后通过 set_url() 方法设置了 robots.txt 的链接。

当然,不用这个方法的话,可以在声明时直接用如下方法设置:

rp = urllib.robotparser.RobotFileParser('https://www.taobao.com/robots.txt')

接着利用 can_fetch() 方法判断了网页是否可以被抓取。

小结

本篇内容到这里就结束了,内容比较简单,希望各位同学以后在写爬虫时候可以遵循 Robot 协议,为了自己的安全着想。

希望各位同学可以自己动手实际操作试试看。

示例代码

本系列的所有代码小编都会放在代码管理仓库 Github 和 Gitee 上,方便大家取用。

示例代码-Github

示例代码-Gitee

参考

https://www.cnblogs.com/zhangxinqi/p/9170312.html

Guess you like

Origin www.cnblogs.com/babycomeon/p/12020258.html