2017-06-13 12 views
2

私はPythonとScrapyを使ってWebクローラー/スクレーパーを作っています。いくつかのWebサイトはコンテンツを動的に読み込むので、PhantomJと組み合わせてSeleniumも使用しています。今、私はこれを使用し始めたとき、パフォーマンスは受け入れられると思っていましたが、かなり遅くなっています。今私はそれが私のコードのいくつかの抜け穴のため、または私が使用しているフレームワーク/プログラムが十分に最適化されていないためであるかどうかはわかりません。だから私はあなたに私がパフォーマンスを向上させるためにできることについての提案について男にお尋ねします。
私が書いたコードは約です。開始と終了まで35秒。約11件のGET要求と3件のPost要求を実行しています。Python/Scrapy/Selenium/PhantomJs - パフォーマンス

import scrapy 
from scrapy.http.request import Request 
from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.common.exceptions import TimeoutException 
from selenium.webdriver.support.ui import WebDriverWait 
import time 


class TechcrunchSpider(scrapy.Spider): 
    name = "techcrunch_spider_performance" 
    allowed_domains = ['techcrunch.com'] 
    start_urls = ['https://techcrunch.com/search/heartbleed'] 



    def __init__(self): 
     self.driver = webdriver.PhantomJS() 
     self.driver.set_window_size(1120, 550) 
     #self.driver = webdriver.Chrome("C:\Users\Daniel\Desktop\Sonstiges\chromedriver.exe") 
     self.driver.wait = WebDriverWait(self.driver, 5) #wartet bis zu 5 sekunden 

    def parse(self, response): 
     start = time.time()  #ZEITMESSUNG 
     self.driver.get(response.url) 

     #wartet bis zu 5 sekunden(oben definiert) auf den eintritt der condition, danach schmeist er den TimeoutException error 
     try:  

      self.driver.wait.until(EC.presence_of_element_located(
       (By.CLASS_NAME, "block-content"))) 
      print("Found : block-content") 

     except TimeoutException: 
      self.driver.close() 
      print(" block-content NOT FOUND IN TECHCRUNCH !!!") 


     #Crawle durch Javascript erstellte Inhalte mit Selenium 

     ahref = self.driver.find_elements(By.XPATH,'//h2[@class="post-title st-result-title"]/a') 

     hreflist = [] 
     #Alle Links zu den jeweiligen Artikeln sammeln 
     for elem in ahref : 
      hreflist.append(elem.get_attribute("href")) 


     for elem in hreflist : 
      print(elem) 



     print("im closing myself") 
     self.driver.close() 
     end = time.time() 
     print("Time elapsed : ") 
     finaltime = end-start 
     print(finaltime) 

私は、Windows 8 64ビットインテルi7-3630QMのCPUの@の2,4GHZ、のNVIDIA GeForce GT 650M、8ギガバイトのRAMを使用しています。
PS:ドイツ語のコメントに申し訳ありません

+1

あなたのスパイダーからAJAXリクエストを生成すると、Seleniumの必要性がなくなり、ページが読み込まれるまで5秒待つ必要がなくなります。この[頻繁な投稿](https://stackoverflow.com/questions/8550114/can-scrapy-be-used-to-scrape-dynamic-content-from-websites-that-are-using-ajax)を確認してください。 – rongon

+1

この質問の回答を読むhttps://stackoverflow.com/questions/39036137/how-yo-make-a-selenium-scripts-faster – parik

答えて

1

代わりにを使用して、Javascriptでページを処理してみてください。

2

私は同じ問題に直面していましたが、毎分2つのURLしか処理されませんでした。

このようにしてWebページをキャッシュします。

...... 
options = ['--disk-cache=true'] 
self.driver = webdriver.PhantomJS(service_args=options) 
...... 

これは、1分あたり2から11までのURL処理をシュートアップします。これは、ウェブページからウェブページに至るまで非常に多くの場合があります。

イメージローディングを無効にして、セレンのページ読み込みを高速化するには、--load-images=falseオプションを追加してください。

希望します。