在未认证的网站上爬行 [英] Crawling on uncerficated website

查看:59
本文介绍了在未认证的网站上爬行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 和 Scrapy 的新手,但我试图从一个证书不受信任的网站上获取一些数据,我认为这就是我无法抓取它的原因,尽管我可能只是错误地做了蜘蛛

I am both new to Python and Scrapy, but I am trying to get some data from a website that has an untrusted certificate, and I think this is why I can't crawl it, although maybe I just did the spider wrongly

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

Here is the error log I get when I try to crawl

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        

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

If someone could help me I would be incredibly grateful

推荐答案

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

UPDATE: This workaround is for Scrapy<1.1. Newer version have a setting to change the tls method, see http://doc.scrapy.org/en/1.1/topics/settings.html?highlight=context#downloader-client-tls-method

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

My answer from the scrapy-users list:

有时 openssl 的协商方法与服务器不兼容.共有三种可用的方法:TLSv1、SSLv2 和 SSLv3(或 SSLv23).

Sometimes the openssl's negotiation metho don't get along with the server. There are three available methods: TLSv1, SSLv2 and SSLv3 (or SSLv23).

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

When the ssl handshake fails for some reason you can discard if it's directly related to the negotiation method using 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

Scrapy uses TLSv1 by default. See https://github.com/scrapy/scrapy/blob/master/scrapy/core/downloader/contextfactory.py#L13

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

A workaround is to provide your own client context factory and set the negotiation method that works with the website. For example:

# 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'

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

You can test that this work by simply running:

$ scrapy shell https://matriculaweb.unb.br/matriculaweb/graduacao/oferta_campus.aspx

这篇关于在未认证的网站上爬行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆