Scrapy+Chromium+代理+
- 2020 年 1 月 2 日
- 筆記
上周說到scrapy的基本入門。這周來寫寫其中遇到的代理和js渲染的坑。
js渲染
js是爬蟲中畢竟麻煩處理的一塊。通常的解決辦法是通過抓包,然後查看request
信息,接着捕獲ajax返回的消息。 但是,如果遇到一些js渲染特別複雜的情況,這種辦法就非常非常的麻煩。所以我們採用了selenium
這個包,用它來調用chromium
完成js渲染的問題。
安裝
tip:為什麼選擇
chromium
而不是chrome
。我之前裝的就是chrome
。但是安裝chrome
之後還需要安裝chrome-drive
,而很多linux發行版的包管理沒有現成的chrome
包和chrome-drive
包,自己去找的話很容易出現chrome-drive
和chrome
版本不一致而導致不能使用。
為了減少因為安裝環境所帶來的煩惱。我們這邊用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:這邊還有一個坑,
chrome
和chromium
都不能在root模式下運行,而且也不安全。所以最好是創建一個用戶來運行。使用docker的時候,run
時候需要加--privileged
參數
如果你需要了解如何在root用戶下運行chrome,請閱讀這篇博文 Ubuntu16.04安裝Chrome瀏覽器及解決root不能打開的問題
requirements.txt
Scrapy selenium Twisted PyMysql pyvirtualdisplay
把requirements.txt
和Dockerfile
放在一起。 並在目錄下使用docker命令docker build -t "chromium-scrapy-image" .
至於為什麼要安裝
xvfb
和pyvirtualdisplay
。因為chromium
的headless
模式下不能處理帶賬號密碼的問題。待會就會說到了。
Redhat
和Debian
可以去包倉庫找一下最新的chromium
和對應的chromium-drive
下載安裝就可以了。版本一定要是對應的!這邊使用chromium=68.0.3440.75-r0
和chromium-chromedriver=68.0.3440.75-r0
。
修改Scrapy
的Middleware
使用了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
。
這就解決了selenium
和chromium
的安裝問題。
chromium
不支持headless
問題
如果你安裝的chromium
版本太老,不支持headless
,不着急。之前我們安裝的xvfb
和pyvirtualdisplay
就派上用場了。
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
。在之前已經提到過,使用xvfb
和pyvirtualdisplay
就可以了。