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

相關文章
相關標籤/搜索