在未经批准的网站上爬行

时间:2013-10-24 23:44:10

标签: python scrapy

我是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        

如果有人能帮助我,我会非常感激

1 个答案:

答案 0 :(得分:10)

更新:此解决方法适用于Scrapy&lt; 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
相关问题