win 7環境scrapy統合selenium動的Webページへのアクセス


詳細
scrapyは静的ページを登ることができますが、jsダイナミックロードで処理されるサイトが増えています.この部分のデータを這い出すにはjs処理の動的ページを少なく処理しなければならない.簡単な方法はjs処理ツールを統合することであり,筆者がここで選んだのはseleniumである.
scrapyのインストールは筆者の他の文章を参照してください.ここで筆者が使っているのはwin 7 64ビット環境です.
python環境ではseleniumをインストールするのが簡単で、コマンドを使用して最新バージョンのseleniumを自動的に取得することができ、筆者がインストールしたのはselenium 3.0である.2、詳しくはhttps://pypi.python.org/pypi/selenium/3.0.2:
pip install selenium

seleniumをインストールするには、各ブラウザのdriverをインストールする必要があります.seleniumは正常に使用できます.筆者は主流ブラウザie、Firefox、chromeについて例を挙げます.各ブラウザのバージョンについて、詳しくはhttp://docs.seleniumhq.org/download/ :
1、IEブラウザIEDriverServer
seleniumの公式サイトはwin 7 64ビットのIEDriverServerのダウンロードリンクを提供します:http://selenium-release.storage.googleapis.com/2.53/IEDriverServer_x64_2.53.1.zip
ダウンロードして解凍した後に使うことができます:
 
iedriver = "D:\scrapy\selenium\driver\IEDriverServer.exe"
driver = webdriver.Ie(iedriver)

2、chromeブラウザchromedriver
 
ダウンロードリンク:http://chromedriver.storage.googleapis.com/2.25/chromedriver_win32.zip
ダウンロードして解凍した後に使うことができます:
 
chromedriver = "D:\scrapy\selenium\driver\chromedriver.exe"
driver = webdriver.Chrome(chromedriver)

3、Firefoxブラウザgeckodriver
 
ダウンロードリンク:https://github.com/mozilla/geckodriver/releases
ダウンロードして解凍した後に使うことができます:
 
firefoxdriver = "D:\scrapy\selenium\driver\geckodriver.exe"
binary = FirefoxBinary("C:\Program Files (x86)\Mozilla Firefox\Firefox.exe")
driver = webdriver.Firefox(executable_path=firefoxdriver,firefox_binary=binary)

注意:firefoxブラウザのパスを指定する必要があります.指定しないとエラーが発生します.
 
WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
——————————————————————————————————————————————
以上はseleniumの環境構築です.次にseleniumとscrapyの統合を開始します.
一般的にseleniumをscrapyフレームワークに統合する方法は2つあります.
1.Middlewareを作成し、Middlewareでseleniumを呼び出して動的にロードする.しかし、この方式はすべての接続を無差別にseleniumでロードするため、柔軟性が悪すぎてscrapyの稼働効率が遅くなり、推奨されません.
2、scrapyのSpiderで必要なページに対してseleniumを呼び出して動的にロードする.この方法は柔軟で、特定のサイトに対する操作もできます.以下、主にこの方式について説明する.
from scrapy.selector import Selector
#  selenium    
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

……


#   selenium  
	def __init__(self):
		CrawlSpider.__init__(self)  
		#firefox
		firefoxdriver = "D:\scrapy\selenium\driver\geckodriver.exe"
		binary = FirefoxBinary("C:\Program Files (x86)\Mozilla Firefox\Firefox.exe")
		self.driver = webdriver.Firefox(executable_path=firefoxdriver,firefox_binary=binary)

		#           
		self.driver.set_page_load_timeout(10)
		self.driver.maximize_window()

	def __del__(self):
		self.driver.close()

……

#    
	def parse_item(self, response):
		print response.url		
		try:
		    self.driver.get(response.url)
		except TimeoutException:
		    #print 'time out after 10 seconds when loading page'
		    self.driver.execute_script('window.stop()') #             ,    Javascript stop  ,        
		……
		#   scrapy Selector        ,     
		#    selenium       ,     selenium     
		sel = Selector(text = self.driver.page_source)

これによりseleniumでjs動的データをロードし、scrapyのページ処理方式に従うことができます.