2

我是 Python 和 Scrapy 的新手,但我正在尝试从具有不受信任证书的网站获取一些数据,我认为这就是我无法抓取它的原因,尽管也许我只是错误地使用了蜘蛛

这是我尝试抓取时得到的错误日志

2013-10-24 21:19:08-0200 [scrapy] INFO: Scrapy 0.18.4 started (bot: tutorial)
2013-10-24 21:19:08-0200 [scrapy] DEBUG: Optional features available: ssl, http11, libxml2
2013-10-24 21:19:08-0200 [scrapy] DEBUG: Overridden settings: {'NEWSPIDER_MODULE': 'tutorial.spiders', 'SPIDER_MODULES': ['tutorial.spiders'], 'BOT_NAME': 'tutorial'}
2013-10-24 21:19:12-0200 [scrapy] DEBUG: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2013-10-24 21:19:15-0200 [scrapy] DEBUG: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2013-10-24 21:19:15-0200 [scrapy] DEBUG: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2013-10-24 21:19:15-0200 [scrapy] DEBUG: Enabled item pipelines: 
2013-10-24 21:19:15-0200 [tutorial] INFO: Spider opened
2013-10-24 21:19:15-0200 [tutorial] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2013-10-24 21:19:15-0200 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2013-10-24 21:19:15-0200 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2013-10-24 21:19:16-0200 [tutorial] DEBUG: Redirecting (301) to <GET https://matriculaweb.unb.br/matriculaweb/graduacao/oferta_campus.aspx> from <GET http://matriculaweb.unb.br/matriculaweb/graduacao/oferta_campus.aspx>
2013-10-24 21:19:16-0200 [tutorial] DEBUG: Retrying <GET https://matriculaweb.unb.br/matriculaweb/graduacao/oferta_campus.aspx> (failed 1 times): [<twisted.python.failure.Failure <class 'OpenSSL.SSL.Error'>>]
2013-10-24 21:19:17-0200 [tutorial] DEBUG: Retrying <GET https://matriculaweb.unb.br/matriculaweb/graduacao/oferta_campus.aspx> (failed 2 times): [<twisted.python.failure.Failure <class 'OpenSSL.SSL.Error'>>]
2013-10-24 21:19:17-0200 [tutorial] DEBUG: Gave up retrying <GET https://matriculaweb.unb.br/matriculaweb/graduacao/oferta_campus.aspx> (failed 3 times): [<twisted.python.failure.Failure <class 'OpenSSL.SSL.Error'>>]
2013-10-24 21:19:17-0200 [tutorial] ERROR: Error downloading <GET https://matriculaweb.unb.br/matriculaweb/graduacao/oferta_campus.aspx>: [<twisted.python.failure.Failure <class 'OpenSSL.SSL.Error'>>]
2013-10-24 21:19:17-0200 [tutorial] INFO: Closing spider (finished)
2013-10-24 21:19:17-0200 [tutorial] INFO: Dumping Scrapy stats:
    {'downloader/exception_count': 3,
     'downloader/exception_type_count/scrapy.xlib.tx._newclient.ResponseNeverReceived': 3,
     'downloader/request_bytes': 1064,
     'downloader/request_count': 4,
     'downloader/request_method_count/GET': 4,
     'downloader/response_bytes': 384,
     'downloader/response_count': 1,
     'downloader/response_status_count/301': 1,
     'finish_reason': 'finished',
     'finish_time': datetime.datetime(2013, 10, 24, 23, 19, 17, 283862),
     'log_count/DEBUG': 10,
     'log_count/ERROR': 1,
     'log_count/INFO': 3,
     'scheduler/dequeued': 4,
     'scheduler/dequeued/memory': 4,
     'scheduler/enqueued': 4,
     'scheduler/enqueued/memory': 4,
     'start_time': datetime.datetime(2013, 10, 24, 23, 19, 15, 955787)}
2013-10-24 21:19:17-0200 [tutorial] INFO: Spider closed (finished)

这是我的代码

from __future__ import absolute_import
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.item import Item

class MySpider(CrawlSpider):
    name = 'tutorial'
    allowed_domains = ['matriculaweb.unb.br']
    start_urls = ['http://matriculaweb.unb.br/matriculaweb/graduacao/oferta_campus.aspx']

    rules = [Rule(SgmlLinkExtractor(allow=('/oferta_dis.aspx?cod=\d+'))),Rule(SgmlLinkExtractor(allow=('/oferta_dados.aspx?cod=\d+&dep=\d+')), 'parse_dep')]

    def parse_dep(self, response):
        sel = Selector(response)
        discplina = Disciplina()

        url_disciplina = '/html/body/center/table/tbody/tr/td/table[4]/tbody/tr/td[2]/div/center/table/tbody/tr/td/font/strong/a'.re(r'.*cod=([0-9]+)')
        yield Request(url_disciplina, meta={'disc':disciplina}, callback=self.parse_disc)


    def parse_disc(self, response):
        sel = Selector(response)
        disciplina = response.request.meta['disc']

        disciplina['nome'] = sel.xpath('/html/body/center/table/tbody/tr/td/center/table/tbody/tr[3]/td[2]').extract()
        disciplina['codigo'] = sel.xpath('/html/body/center/table/tbody/tr/td/center/table/tbody/tr[2]/td[2]').extract()
        disciplina['requisitos'] = sel.xpath('/html/body/center/table/tbody/tr/td/center/table/tbody/tr[6]/td[2]').extract()

        yield disciplina        

如果有人可以帮助我,我将非常感激

4

1 回答 1

10

更新:此解决方法适用于 Scrapy<1.1。较新版本具有更改 tls 方法的设置,请参阅http://doc.scrapy.org/en/1.1/topics/settings.html?highlight=context#downloader-client-tls-method

我在 scrapy-users 列表中的回答:

有时,openssl 的协商方法与服务器无法相处。共有三种可用方法:TLSv1、SSLv2 和 SSLv3(或 SSLv23)。

当 ssl 握手由于某种原因失败时,如果它与使用 curl 的协商方法直接相关,则可以丢弃:

 $ curl --tlsv1 -k https://matriculaweb.unb.br/matriculaweb/graduacao/oferta_campus.aspx

 $ curl --sslv2 -k https://matriculaweb.unb.br/matriculaweb/graduacao/oferta_campus.aspx

 $ curl --sslv3 -k https://matriculaweb.unb.br/matriculaweb/graduacao/oferta_campus.aspx

Scrapy 默认使用 TLSv1。见https://github.com/scrapy/scrapy/blob/master/scrapy/core/downloader/contextfactory.py#L13

一种解决方法是提供您自己的客户端上下文工厂并设置适用于网站的协商方法。例如:

# file: myproject/contextfactory.py
from OpenSSL import SSL
from scrapy.core.downloader.contextfactory import ScrapyClientContextFactory


class MyClientContextFactory(ScrapyClientContextFactory):
    def __init__(self):
        self.method = SSL.SSLv23_METHOD  # or SSL.SSLv3_METHOD

并设置设置:

DOWNLOADER_CLIENTCONTEXTFACTORY = 'myproject.contextfactory.MyClientContextFactory'

您可以通过简单地运行来测试这项工作:

$ scrapy shell https://matriculaweb.unb.br/matriculaweb/graduacao/oferta_campus.aspx
于 2013-10-25T23:55:59.257 回答