Scrapy+Chromium+代理+

上周说到scrapy的基本入门。这周来写写其中遇到的代理和js渲染的坑。

js渲染

js是爬虫中毕竟麻烦处理的一块。通常的解决办法是通过抓包,然后查看request信息,接着捕获ajax返回的消息。 但是,如果遇到一些js渲染特别复杂的情况,这种办法就非常非常的麻烦。所以我们采用了selenium这个包,用它来调用chromium完成js渲染的问题。

安装

  1. 安装selenium
  2. 安装chromium
  3. 安装chromium-drive

tip:为什么选择chromium而不是chrome。我之前装的就是chrome。但是安装chrome之后还需要安装chrome-drive,而很多linux发行版的包管理没有现成的chrome包和chrome-drive包,自己去找的话很容易出现chrome-drivechrome版本不一致而导致不能使用。

为了减少因为安装环境所带来的烦恼。我们这边用docker来解决。 Dockerfile

FROM alpine:3.8  COPY requirements.txt /tmp  RUN apk update       && apk add --no-cache xvfb python3 python3-dev curl libxml2-dev libxslt-dev libffi-dev gcc musl-dev       && apk add --no-cache libgcc openssl-dev chromium=68.0.3440.75-r0 libexif udev chromium-chromedriver=68.0.3440.75-r0       && curl https://bootstrap.pypa.io/get-pip.py | python3       && adduser -g chromegroup -D chrome       && pip3 install -r /tmp/requirements.txt && rm /tmp/requirements.txt  USER chrome

tip:这边还有一个坑,chromechromium都不能在root模式下运行,而且也不安全。所以最好是创建一个用户来运行。使用docker的时候,run时候需要加--privileged参数

如果你需要了解如何在root用户下运行chrome,请阅读这篇博文 Ubuntu16.04安装Chrome浏览器及解决root不能打开的问题

requirements.txt

Scrapy  selenium  Twisted  PyMysql  pyvirtualdisplay

requirements.txtDockerfile放在一起。 并在目录下使用docker命令docker build -t "chromium-scrapy-image" .

至于为什么要安装xvfbpyvirtualdisplay。因为chromiumheadless模式下不能处理带账号密码的问题。待会就会说到了。


RedhatDebian可以去包仓库找一下最新的chromium和对应的chromium-drive下载安装就可以了。版本一定要是对应的!这边使用chromium=68.0.3440.75-r0chromium-chromedriver=68.0.3440.75-r0


修改ScrapyMiddleware

使用了chromium之后,我们在middlewares.py文件修改一下。我们的设想是让chromium来替代掉request请求。所以我们修改了DownloaderMiddleware

#DownloaderMiddleware  class DemoDownloaderMiddleware(object):      def __init__(self):          chrome_options = webdriver.ChromeOptions()          # 启用headless模式          chrome_options.add_argument('--headless')          # 关闭gpu          chrome_options.add_argument('--disable-gpu')          # 关闭图像显示          chrome_options.add_argument('--blink-settings=imagesEnabled=false')          self.driver = webdriver.Chrome(chrome_options=chrome_options)        def __del__(self):          self.driver.quit()        @classmethod      def from_crawler(cls, crawler):          s = cls()          crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)          return s        def process_request(self, request, spider):          # chromium处理          # ...          return HtmlResponse(url=request.url,          body=self.driver.page_source,          request=request,          encoding='utf-8',          status=200)        def process_response(self, request, response, spider):          # Called with the response returned from the downloader.            # Must either;          # - return a Response object          # - return a Request object          # - or raise IgnoreRequest          return response        def process_exception(self, request, exception, spider):          # Called when a download handler or a process_request()          # (from other downloader middleware) raises an exception.            # Must either:          # - return None: continue processing this exception          # - return a Response object: stops process_exception() chain          # - return a Request object: stops process_exception() chain          pass        def spider_opened(self, spider):          spider.logger.info('Spider opened: %s' % spider.name)

tip:这边我们只有一个中间件来处理request。也就是说,所有的逻辑都要经过这儿。所以直接返回了response

这就解决了seleniumchromium的安装问题。

chromium不支持headless问题

如果你安装的chromium版本太老,不支持headless,不着急。之前我们安装的xvfbpyvirtualdisplay就派上用场了。

from pyvirtualdisplay import Display  ...  >>>  chrome_options.add_argument('--headless')    <<<  # chrome_options.add_argument('--headless')  display=Display(visible=0,size=(800,800))  display.start()  ...    >>>  self.driver.quit()    <<<  self.driver.quit()  display.stop()  ...

我们模拟出了一个显示界面,这个时候,不管chromium开不开启headless,都能在我们的服务器上运行了。

代理

因为我们已经用chromium替换了request。所以我们做的代理也不能在Scrapy中来处理。 我们需要直接用chromium来处理IP代理问题。

这是不使用chromium之前使用代理的办法

class DemoProxyMiddleware(object):      # overwrite process request        def process_request(self, request, spider):          # Set the location of the proxy          request.meta['proxy'] = "https://proxy.com:8080"            # Use the following lines if your proxy requires authentication            proxy_user_pass = "username:password"          encoded_user_pass = base64.b64encode(proxy_user_pass.encode('utf-8'))            # setup basic authentication for the proxy          request.headers['Proxy-Authorization'] = 'Basic ' + str(encoded_user_pass, encoding="utf-8")

如果你的IP代理不需要账号密码的话,只需要把后面三行删除了就可以了。

根据上面这段代码,我们也不难猜出chromium解决代理的方法了。

chrome_options.add_argument('--proxy=proxy.com:8080')

只需要加一段argument就可以了。

那解决带账号密码的办法呢?

解决chromium下带账号密码的代理问题

先创建一个py文件

import string  import zipfile      def create_proxyauth_extension(proxy_host, proxy_port,                                 proxy_username, proxy_password,                                 scheme='http', plugin_path=None):      """代理认证插件        args:          proxy_host (str): 你的代理地址或者域名(str类型)          proxy_port (int): 代理端口号(int类型)          proxy_username (str):用户名(字符串)          proxy_password (str): 密码 (字符串)      kwargs:          scheme (str): 代理方式 默认http          plugin_path (str): 扩展的绝对路径        return str -> plugin_path      """        if plugin_path is None:          plugin_path = 'vimm_chrome_proxyauth_plugin.zip'        manifest_json = """      {          "version": "1.0.0",          "manifest_version": 2,          "name": "Chrome Proxy",          "permissions": [              "proxy",              "tabs",              "unlimitedStorage",              "storage",              "<all_urls>",              "webRequest",              "webRequestBlocking"          ],          "background": {              "scripts": ["background.js"]          },          "minimum_chrome_version":"22.0.0"      }      """        background_js = string.Template(          """          var config = {                  mode: "fixed_servers",                  rules: {                    singleProxy: {                      scheme: "${scheme}",                      host: "${host}",                      port: parseInt(${port})                    },                    bypassList: ["foobar.com"]                  }                };            chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});            function callbackFn(details) {              return {                  authCredentials: {                      username: "${username}",                      password: "${password}"                  }              };          }            chrome.webRequest.onAuthRequired.addListener(                      callbackFn,                      {urls: ["<all_urls>"]},                      ['blocking']          );          """      ).substitute(          host=proxy_host,          port=proxy_port,          username=proxy_username,          password=proxy_password,          scheme=scheme,      )      with zipfile.ZipFile(plugin_path, 'w') as zp:          zp.writestr("manifest.json", manifest_json)          zp.writestr("background.js", background_js)        return plugin_path

使用方式

    proxyauth_plugin_path = create_proxyauth_extension(          proxy_host="host",          proxy_port=port,          proxy_username="user",          proxy_password="pwd")      chrome_options.add_extension(proxyauth_plugin_path)

这样就完成了chromium的代理了。但是,如果你开启了headless模式,这个方法会提示错误。所以解决办法就是,关闭headless模式。 至于怎么在没有gui的情况下使用chromium。在之前已经提到过,使用xvfbpyvirtualdisplay就可以了。