Python/Scrapy/Selenium/PhantomJs - 性能

标签 python performance selenium scrapy phantomjs

我正在使用 Python 和 Scrapy 制作网络爬虫/抓取器。因为有些网站动态加载其内容,所以我还将 Selenium 与 PhantomJs 结合使用。现在,当我开始使用它时,我认为性能是可以接受的,但事实证明它非常慢。现在我不确定这是否是因为我的代码中存在一些漏洞,或者因为我正在使用的框架/程序没有足够优化。所以我向你们询问有关我可以采取哪些措施来提高性能的建议。
我写的代码大约需要。开始和结束 35 秒。它执行大约 11 个 GET 请求和 3 个 Post 请求。

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 位、intel i7-3630QM CPU @ 2,4GHZ、Nvidia Geforce GT 650M、8GB RAM。
PS:对德国评论表示抱歉

最佳答案

我也面临着同样的问题,每分钟只处理 2 个网址。

我通过这样做来缓存网页。

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

这将 URL 处理量从每分钟 2 次增加到 11 次,以防万一。这可能因网页而异。

如果您想禁用图像加载以加快 Selenium 中的页面加载速度,请将 --load-images=false 添加到上面的选项中。

希望有帮助。

关于Python/Scrapy/Selenium/PhantomJs - 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44515734/

相关文章:

python - 安装 Twisted for Python 时出错

python/pandas 将列折叠为日期时间季度

selenium - 如何通过 Linux VM 运行 InternetExplorerDriver?

firefox - 关闭 Firefox 13 中的内置 RSS 阅读器

php - 我怎样才能使我的数据库响应时间和渲染时间更好?

python - 使用 Selenium Python 的复选框

python - 根据系列或数组中的索引访问 pandas 字符串列字符

python - 值错误 : exog does not have full column rank

c++ - 传递 const 引用 : performance increase?

python - 加速python的请求函数