【问题标题】:python.failure.Failure OpenSSL.SSL.Error in Scrapy (version 1.0.4)Scrapy 中的 python.failure.Failure OpenSSL.SSL.Error(版本 1.0.4)
【发布时间】:2016-04-24 19:56:40
【问题描述】:

我正在做一个数据抓取项目,我的代码使用 Scrapy(版本 1.0.4)和 Selenium(版本 2.47.1)。

from scrapy import Spider
from scrapy.selector import Selector
from scrapy.http import Request
from scrapy.spiders import CrawlSpider
from selenium import webdriver

class TradesySpider(CrawlSpider):
    name = 'tradesy'
    start_urls = ['My Start url',]

    def __init__(self):
        self.driver = webdriver.Firefox()

    def parse(self, response):
        self.driver.get(response.url)
        while True:
           tradesy_urls = Selector(response).xpath('//div[@id="right-panel"]"]')
           data_urls = tradesy_urls.xpath('div[@class="item streamline"]/a/@href').extract()
           for link in data_urls:
               url = 'My base url'+link
               yield Request(url=url,callback=self.parse_data)
               time.sleep(10)
           try:
               data_path = self.driver.find_element_by_xpath('//*[@id="page-next"]')
           except:
               break
           data_path.click()
           time.sleep(10)

    def parse_data(self,response):
        'Scrapy Operations...'

当我执行我的代码时,我得到了一些 url 的预期输出,但对于其他的我得到了以下错误。

2016-01-19 15:45:17 [scrapy] DEBUG: Retrying <GET MY_URL> (failed 1 times): [<twisted.python.failure.Failure OpenSSL.SSL.Error: [('SSL routines', 'SSL3_READ_BYTES', 'ssl handshake failure')]>]

请为此查询提供解决方案。

【问题讨论】:

    标签: python-2.7 ssl scrapy


    【解决方案1】:

    根据这个reported issue,您可以创建自己的ContextFactory 来处理SSL。

    context.py:

    from OpenSSL import SSL
    from scrapy.core.downloader.contextfactory import ScrapyClientContextFactory
    
    
    class CustomContextFactory(ScrapyClientContextFactory):
        """
        Custom context factory that allows SSL negotiation.
        """
    
        def __init__(self):
            # Use SSLv23_METHOD so we can use protocol negotiation
            self.method = SSL.SSLv23_METHOD
    

    settings.py

    DOWNLOADER_CLIENTCONTEXTFACTORY = 'yourproject.context.CustomContextFactory'
    

    【讨论】:

    • 这解决了我的问题! (读者请注意,当然,您需要更改上面粘贴的“yourproject”部分)。谢谢,eLRuLL!!
    • 很高兴帮助@Chris。 Wineartist 如果对您有帮助,请记住接受答案。谢谢
    • 它也适用于pyOpenSSL==17.5.0 & Scrapy==1.0.3
    【解决方案2】:

    使用 Scrapy 1.5.0 我遇到了这个错误:

    Error downloading: https://my.website.com>: [<twisted.python.failure.Failure OpenSSL.SSL.Error: [('SSL routines', 'tls12_check_peer_sigalg', 'wrong curve')]>]
    

    最终的工作是更新我的 Twisted 版本(从 17.9.0 -> 19.10.0)。我还将 Scrapy 更新到 2.4.0 和其他一些:

    • 密码学==2.2.2 -> 2.3
    • parsel==1.4.0 -> 1.5.0
    • pyOpenSSL==17.5.0 -> 19.0.0
    • urllib3==1.22 -> 1.24.3

    【讨论】:

      【解决方案3】:

      不需要额外文件的 eLRuLL 答案的变体。它“装饰”了 ScrapyClientContextFactory 类的 init 方法。

      from OpenSSL import SSL
      from scrapy.core.downloader.contextfactory import ScrapyClientContextFactory
      
      init = ScrapyClientContextFactory.__init__ 
      def init2(self, *args, **kwargs):
        init(self, *args, **kwargs)
        self.method = SSL.SSLv23_METHOD
      ScrapyClientContextFactory.__init__ = init2
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-26
        • 1970-01-01
        • 2014-02-14
        • 2014-07-20
        • 2014-12-31
        • 1970-01-01
        相关资源
        最近更新 更多