Scrapy 探索:如何防止爬虫被禁止(翻译、转载、整理)
2017-10-30 10:32
363 查看
一、引言
曾经在写一个百度关键词爬虫的时候,发现返回的 response 数据异常,最后发现是被百度 ban 掉了我的访问。那么如何防止爬虫被禁止呢?
Scrapy 的官方文档给出了答案,这里我转载一下,记录一下。
这是原文地址:
Scrapy: Avoiding getting banned
二、Avoiding getting banned
Some websites implement certain measures to prevent bots from crawling them, with varying degrees of sophistication. Getting around those measures can be difficult and tricky, and may sometimes require special infrastructure. Please consider contacting commercial support if in doubt.有些网站会使用一些复杂的措施来防止爬虫。突破这些防线会有些复杂,可能还需要一些高深的知识技能。如有疑惑可以联系客服。
就我所知,只要是网站对于自己的数据有些保护的,并且规模比较大的都会有防爬虫机制。
Here are some tips to keep in mind when dealing with these kinds of sites:
以下将列举出一些方法来处理这些网站:
以下的方法都不仅仅限于 Scrapy,可以说所有的爬虫都可以用以下方法来反反爬虫机制:
1. User Agent Pool
rotate your user agent from a pool of well-known ones from browsers (google around to get a list of them)
建立一个用户代理池用来轮询使用(Google 一下能搜到很多)
这一个方法有很多参考博客可以查看,这里我推荐这个问答进行查看:
Scrapy多个USER_AGENT切换使用应该怎么做呢?
这里面推荐了一个 GitHub 上的实例:
scrapy-random-useragent
也就是说,我们通过在发起每次请求的时候,都使用随机的 User Agent,用这样的方式来提高爬虫的隐蔽性。
2. Disable cookies
disable cookies (see COOKIES_ENABLED) as some sites may use cookies to spot bot behaviour
禁用 cookies(查看 COOKIES_ENABLED 字段),有些网站使用 cookies 来标记一个爬虫
影射到 Scrapy 中,就是在 settings.py 中,设置 COOKIES_ENABLED 字段为 False
3. Download delays
use download delays (2 or higher). See DOWNLOAD_DELAY setting.
使用下载延迟(2 或者更高)。查看 DOWLOAD_DELAY 设置项。
影射到 Scrapy 中,就是在 settings.py 中,设置 DOWNLOAD_DELAY 字段为 2 或者更高值。
4. Google cache
if possible, use Google cache to fetch pages, instead of hitting the sites directly
如果可以的话,使用 Google cache 获取页面,而不是直接访问它
Google cache 是什么呢?
Google cache
直接引用它的第一句话来介绍:
Google takes a snapshot of each page it examines and caches(stores) that version as a back-up. The cached version is what Google uses to judge if a page is a good match for your query.
Google 对它检查的每一个页面做一个“快照“,并且将该版本作为备份缓存起来。然后基于这个缓存版本页面来适配你的查询。
也就是说, Google cache 对我们将要访问的页面进行了快速拷贝,我们所要进行的查询都发生在这个备份上面。以此方式来防止反爬虫机制。
5. IP pool
use a pool of rotating IPs. For example, the free Tor project or paid services like ProxyMesh. An open source althernative is scrapoxy, a super proxy that you can attach your own proxies to.
建立一个 IP 池轮询使用。比如,免费项目 Tor project 或者收费项目 ProxiyMesh。另外有一个开源选择 scraposxy,一个你可以连接的超级代理
切换 IP 当然是一个可以使用的方法,有些网站发现了爬虫行为就是直接封 IP 的。
6. Distributed downloader
use a hignly distributed downloader that circumvents bans internally, so you can just focus on parsing clean pages. One example of such downloaders is Crawlera
使用高分布式下载来从内部防止被禁用,这样你就可以专心于解析页面内容的工作了。一个很好的例子就是 Crawlera。
分布式爬虫也是一个很热门的话题,鄙人还未曾接触过,不敢妄言,网上资源很多,可以自行探索。
三、总结
总之,有以上 6 种方式可以供我们参考。就可行性而言,使用用户代理池、禁用 cookies 和下载延迟是比较快速的方法。Google cache 需要学习成本,而且有被墙的可能,IP 池有种依托其他工具的不自由感,分布式爬虫当然有待探索。
这是我的一篇整理笔记,疏漏之处在所难免,还望见谅。
Scrapy 还需要认真学习,To be continue!
相关文章推荐
- Scrapy研究探索(七)——如何防止被ban之策略大集合
- Scrapy研究探索(七)——如何防止被ban之策略大集合
- Scrapy研究探索(七)——如何防止被ban之策略大集合
- Scrapy研究探索(七)——如何防止被ban之策略大集合
- Scrapy研究探索(七)——如何防止被ban之策略大集合
- scrapy爬虫防止被禁止 User Agent切换
- 【转】 [置顶] Scrapy研究探索(七)——如何防止被ban之策略大集合
- Scrapy研究和探索(七)——如何防止被ban大集合策略
- 下scrapy-redis如何为不同的爬虫项目分配不同的db,而不仅仅使用db0
- (非原)SQL注入专题--整理帖 && like 语句拼sql 如何防止注入攻击。
- (9)分布式下的爬虫Scrapy应该如何做-关于ajax抓取的处理(一)
- unity ui button ugui 按钮如何设置点击区域为圆形?【整理转载】
- 在c++中如何用new生成一个构造函数带参数的类数组(转载整理)
- 如何防止网站被爬虫爬取的几种办法
- Verilog综合是wire和reg如何防止被优化(转载)
- [网络整理][转载][记个笔记]利用jquery ajaxPrefilter防止ajax重复提交
- Scrapy 探索:如何使用 Pycharm 研读 Scrapy 源码
- 如何选择创作类型的原创、翻译、转载『博客帮助』
- (1)分布式下的爬虫Scrapy应该如何做-安装