Scrapy+Chromium+代理+selenium

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

js渲染

js是爬虫中毕竟麻烦处理的一块。一般的解决办法是经过抓包,而后查看request信息,接着捕获ajax返回的消息。
可是,若是遇到一些js渲染特别复杂的状况,这种办法就很是很是的麻烦。因此咱们采用了selenium这个包,用它来调用chromium完成js渲染的问题。linux

安装

  1. 安装selenium
  2. 安装chromium
  3. 安装chromium-drive
tip:为何选择 chromium而不是 chrome。我以前装的就是 chrome。可是安装 chrome以后还须要安装 chrome-drive,而不少linux发行版的包管理没有现成的 chrome包和 chrome-drive包,本身去找的话很容易出现 chrome-drivechrome版本不一致而致使不能使用。

为了减小由于安装环境所带来的烦恼。咱们这边用docker来解决。
Dockerfileweb

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参数ajax

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

requirements.txtchrome

Scrapy
selenium
Twisted
PyMysql
pyvirtualdisplay

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

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

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


修改ScrapyMiddleware

使用了chromium以后,咱们在middlewares.py文件修改一下。咱们的设想是让chromium来替代掉request请求。因此咱们修改了DownloaderMiddlewarebootstrap

#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的安装问题。vim

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就能够了。

相关文章
相关标签/搜索