Fork me on GitHub

如何让你的scrapy爬虫不再被ban之二(利用第三方平台crawlera做scrapy爬虫防屏蔽)

  我们在做scrapy爬虫的时候,爬虫经常被ban是常态。然而前面的文章如何让你的scrapy爬虫不再被ban,介绍了scrapy爬虫防屏蔽的各种策略组合。前面采用的是禁用cookies、动态设置user agent、代理IP和VPN等一系列的措施组合来防止爬虫被ban。除此以外官方文档还介绍了采用Google cache和crawlera的方法。这里就着重介绍一下如何利用crawlera来达到爬虫不被ban的效果。crawlera是一个利用代理IP地址池来做分布式下载的第三方平台,除了scrapy可以用以外,普通的java、php、python等都可以通过curl的方式来调用。好了,下面入正题。

  说明:

  本文章是基于前面的一系列文章完成的,如果您错过了。可以在此查看:

  安装python爬虫scrapy踩过的那些坑和编程外的思考

  scrapy爬虫成长日记之创建工程-抽取数据-保存为json格式的数据

  scrapy爬虫成长日记之将抓取内容写入mysql数据库

  如何让你的scrapy爬虫不再被ban

  crawlera官方网址:http://scrapinghub.com/crawlera/

  crawlera帮助文档:http://doc.scrapinghub.com/crawlera.html

  一、注册crawlera账号,获取crawlera API KEY

  1、注册一个crawlera账号并激活

  https://dash.scrapinghub.com/account/signup/

  

  填写好用户名,邮件和密码点击sign up即完成注册,收到注册确认邮件确认即可。

  2、创建一个Organizations

  

  3、创建完Organizations后添加crawlera user

    

  

  4、查看API key

  

  点击crawlera user的名称jack就可以查看API的详细信息了(key

  

 

  至此,crawlera API的信息已经获取到了。

  二、修改scrapy项目

  下面看看怎么添加到scrapy项目

  1、安装scrapy-crawlera

pip install scrapy-crawlera

  2、修改settings.py

  DOWNLOADER_MIDDLEWARES下添加配置项

'scrapy_crawlera.CrawleraMiddleware': 600 

  其他配置项

CRAWLERA_ENABLED = True
CRAWLERA_USER = '<API key>'
CRAWLERA_PASS = '你crawlera账号的密码'

  注意:由于之前的项目用了自定义代理的方式,因此DOWNLOADER_MIDDLEWARES下的

#'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware': 110, #代理需要用到
#'cnblogs.middlewares.ProxyMiddleware': 100, #代理需要用到

  这两个配置项要注释掉。

  3、测试crawlera的抓取是否可用

scrapy crawl CnblogsSpider

  4、查看结果

  

  这里可以看到crawlera已经正常工作了。

  5、另外crawlera官网也可以查看抓取结果

  

  scrapy运用crawlera进行抓取就介绍到这里。另外crawlera还提供付费定制服务,如果经费充足也可以考虑付费定制scrapy的爬虫。

  代码更新至此:https://github.com/jackgitgz/CnblogsSpider提交到github的代码将api和password去掉了,如果想运行需要添加自己的key和password

  三、题外话:

  如果你不是scrapy爬虫,而仅仅是想python调用,crawlera也提供了python直接调用的方法

  1、通过request的方式

import requests

url = "http://twitter.com"
proxy = "paygo.crawlera.com:8010"
proxy_auth = "<API KEY>:"

proxies = {
    "http": "http://{0}@{1}/".format(proxy_auth, proxy)
}

headers = {
    "X-Crawlera-Use-HTTPS": 1
}

r = requests.get(url, proxies=proxies, headers=headers)

print("""
Requesting [{}]
through proxy [{}]

Response Time: {}
Response Code: {}
Response Headers:
{}

Response Body:
{}
""".format(url, proxy, r.elapsed.total_seconds(), r.status_code, r.headers, r.text))

  2、request代理重写url

import requests
from requests.auth import HTTPProxyAuth

url = "https://twitter.com"
headers = {}
proxy_host = "paygo.crawlera.com"
proxy_auth = HTTPProxyAuth("<API KEY>", "")
proxies = {"http": "http://{}:8010/".format(proxy_host)}

if url.startswith("https:"):
    url = "http://" + url[8:]
    headers["X-Crawlera-Use-HTTPS"] = "1"

r = requests.get(url, headers=headers, proxies=proxies, auth=proxy_auth)

print("""
Requesting [{}]
through proxy [{}]

Response Time: {}
Response Code: {}
Response Headers:
{}

Response Body:
{}
""".format(url, proxy_host, r.elapsed.total_seconds(), r.status_code, 
           r.headers, r.text))

  crawlera就介绍到这里,更多关于crawlera的内容可以参考官方文档:http://doc.scrapinghub.com/index.html

posted @ 2015-06-16 23:33  秋楓  阅读(17732)  评论(11编辑  收藏  举报