Scrapy 获取网站时出现错误“DNS 查找失败”

2023-12-02

我正在尝试使用 Scrapy 获取“DNS 查找失败”网站上的所有链接。

问题是,每个没有任何错误的网站都打印在解析对象方法,但当 url 返回 DNS 查找失败时,回调parse_obj 没有被调用.

我想获取所有出现错误的域“DNS 查找失败“, 我怎样才能做到这一点 ?

Logs :

2016-03-08 12:55:12 [scrapy] INFO: Spider opened
2016-03-08 12:55:12 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-03-08 12:55:12 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2016-03-08 12:55:12 [scrapy] DEBUG: Crawled (200) <GET http://domain.com> (referer: None)
2016-03-08 12:55:12 [scrapy] DEBUG: Retrying <GET http://expired-domain.com/> (failed 1 times): DNS lookup failed: address 'expired-domain.com' not found: [Errno 11001] getaddrinfo failed.

Code :

class MyItem(Item):
    url= Field()

class someSpider(CrawlSpider):
    name = 'Crawler'        
    start_urls = ['http://domain.com']
    rules = (Rule(LxmlLinkExtractor(allow=()), callback='parse_obj', follow=True),)

    def parse_obj(self, response):
        item = MyItem()
        item['url'] = []
        for link in LxmlLinkExtractor(allow=()).extract_links(response):
            parsed_uri = urlparse(link.url)
            url = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri)
            print url

CrawlSpider 规则不允许传递错误返回(这是一个耻辱)

这是一个变体另一个答案我给出了捕获 DNS 错误的方法:

# -*- coding: utf-8 -*-
import random

import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.spidermiddlewares.httperror import HttpError
from twisted.internet.error import DNSLookupError
from twisted.internet.error import TimeoutError


class HttpbinSpider(CrawlSpider):
    name = "httpbin"

    # this will generate test links so that we can see CrawlSpider in action
    start_urls = (
        'https://httpbin.org/links/10/0',
    )
    rules = (
        Rule(LinkExtractor(),
             callback='parse_page',
             # hook to be called when this Rule generates a Request
             process_request='add_errback'),
    )

    # this is just to no retry errors for this example spider
    custom_settings = {
        'RETRY_ENABLED': False
    }

    # method to be called for each Request generated by the Rules above,
    # here, adding an errback to catch all sorts of errors
    def add_errback(self, request):
        self.logger.debug("add_errback: patching %r" % request)

        # this is a hack to trigger a DNS error randomly
        rn = random.randint(0, 2)
        if rn == 1:
            newurl = request.url.replace('httpbin.org', 'httpbin.organisation')
            self.logger.debug("add_errback: patching url to %s" % newurl)
            return request.replace(url=newurl,
                                   errback=self.errback_httpbin)

        # this is the general case: adding errback to all requests
        return request.replace(errback=self.errback_httpbin)

    def parse_page(self, response):
        self.logger.info("parse_page: %r" % response)

    def errback_httpbin(self, failure):
        # log all errback failures,
        # in case you want to do something special for some errors,
        # you may need the failure's type
        self.logger.error(repr(failure))

        if failure.check(HttpError):
            # you can get the response
            response = failure.value.response
            self.logger.error('HttpError on %s', response.url)

        elif failure.check(DNSLookupError):
            # this is the original request
            request = failure.request
            self.logger.error('DNSLookupError on %s', request.url)

        elif failure.check(TimeoutError):
            request = failure.request
            self.logger.error('TimeoutError on %s', request.url)

这是您在控制台上看到的内容:

$ scrapy crawl httpbin
2016-03-08 15:16:30 [scrapy] INFO: Scrapy 1.0.5 started (bot: httpbinlinks)
2016-03-08 15:16:30 [scrapy] INFO: Optional features available: ssl, http11
2016-03-08 15:16:30 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'httpbinlinks.spiders', 'SPIDER_MODULES': ['httpbinlinks.spiders'], 'BOT_NAME': 'httpbinlinks'}
2016-03-08 15:16:30 [scrapy] INFO: Enabled extensions: CloseSpider, TelnetConsole, LogStats, CoreStats, SpiderState
2016-03-08 15:16:30 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2016-03-08 15:16:30 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2016-03-08 15:16:30 [scrapy] INFO: Enabled item pipelines: 
2016-03-08 15:16:30 [scrapy] INFO: Spider opened
2016-03-08 15:16:30 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-03-08 15:16:30 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2016-03-08 15:16:30 [scrapy] DEBUG: Crawled (200) <GET https://httpbin.org/links/10/0> (referer: None)
2016-03-08 15:16:31 [httpbin] DEBUG: add_errback: patching <GET https://httpbin.org/links/10/1>
2016-03-08 15:16:31 [httpbin] DEBUG: add_errback: patching <GET https://httpbin.org/links/10/2>
2016-03-08 15:16:31 [httpbin] DEBUG: add_errback: patching <GET https://httpbin.org/links/10/3>
2016-03-08 15:16:31 [httpbin] DEBUG: add_errback: patching <GET https://httpbin.org/links/10/4>
2016-03-08 15:16:31 [httpbin] DEBUG: add_errback: patching <GET https://httpbin.org/links/10/5>
2016-03-08 15:16:31 [httpbin] DEBUG: add_errback: patching url to https://httpbin.organisation/links/10/5
2016-03-08 15:16:31 [httpbin] DEBUG: add_errback: patching <GET https://httpbin.org/links/10/6>
2016-03-08 15:16:31 [httpbin] DEBUG: add_errback: patching <GET https://httpbin.org/links/10/7>
2016-03-08 15:16:31 [httpbin] DEBUG: add_errback: patching <GET https://httpbin.org/links/10/8>
2016-03-08 15:16:31 [httpbin] DEBUG: add_errback: patching <GET https://httpbin.org/links/10/9>
2016-03-08 15:16:31 [httpbin] DEBUG: add_errback: patching url to https://httpbin.organisation/links/10/9
2016-03-08 15:16:31 [scrapy] DEBUG: Crawled (200) <GET https://httpbin.org/links/10/8> (referer: https://httpbin.org/links/10/0)
2016-03-08 15:16:31 [httpbin] ERROR: <twisted.python.failure.Failure twisted.internet.error.DNSLookupError: DNS lookup failed: address 'httpbin.organisation' not found: [Errno -5] No address associated with hostname.>
2016-03-08 15:16:31 [httpbin] ERROR: DNSLookupError on https://httpbin.organisation/links/10/5
2016-03-08 15:16:31 [httpbin] ERROR: <twisted.python.failure.Failure twisted.internet.error.DNSLookupError: DNS lookup failed: address 'httpbin.organisation' not found: [Errno -5] No address associated with hostname.>
2016-03-08 15:16:31 [httpbin] ERROR: DNSLookupError on https://httpbin.organisation/links/10/9
2016-03-08 15:16:31 [httpbin] INFO: parse_page: <200 https://httpbin.org/links/10/8>
2016-03-08 15:16:31 [scrapy] DEBUG: Crawled (200) <GET https://httpbin.org/links/10/7> (referer: https://httpbin.org/links/10/0)
2016-03-08 15:16:31 [scrapy] DEBUG: Crawled (200) <GET https://httpbin.org/links/10/6> (referer: https://httpbin.org/links/10/0)
2016-03-08 15:16:31 [scrapy] DEBUG: Crawled (200) <GET https://httpbin.org/links/10/3> (referer: https://httpbin.org/links/10/0)
2016-03-08 15:16:31 [scrapy] DEBUG: Crawled (200) <GET https://httpbin.org/links/10/4> (referer: https://httpbin.org/links/10/0)
2016-03-08 15:16:31 [scrapy] DEBUG: Crawled (200) <GET https://httpbin.org/links/10/1> (referer: https://httpbin.org/links/10/0)
2016-03-08 15:16:31 [scrapy] DEBUG: Crawled (200) <GET https://httpbin.org/links/10/2> (referer: https://httpbin.org/links/10/0)
2016-03-08 15:16:31 [httpbin] INFO: parse_page: <200 https://httpbin.org/links/10/7>
2016-03-08 15:16:31 [httpbin] INFO: parse_page: <200 https://httpbin.org/links/10/6>
2016-03-08 15:16:31 [httpbin] INFO: parse_page: <200 https://httpbin.org/links/10/3>
2016-03-08 15:16:31 [httpbin] INFO: parse_page: <200 https://httpbin.org/links/10/4>
2016-03-08 15:16:31 [httpbin] INFO: parse_page: <200 https://httpbin.org/links/10/1>
2016-03-08 15:16:31 [httpbin] INFO: parse_page: <200 https://httpbin.org/links/10/2>
2016-03-08 15:16:31 [scrapy] INFO: Closing spider (finished)
2016-03-08 15:16:31 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/exception_count': 2,
 'downloader/exception_type_count/twisted.internet.error.DNSLookupError': 2,
 'downloader/request_bytes': 2577,
 'downloader/request_count': 10,
 'downloader/request_method_count/GET': 10,
 'downloader/response_bytes': 3968,
 'downloader/response_count': 8,
 'downloader/response_status_count/200': 8,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2016, 3, 8, 14, 16, 31, 761515),
 'log_count/DEBUG': 20,
 'log_count/ERROR': 4,
 'log_count/INFO': 14,
 'request_depth_max': 1,
 'response_received_count': 8,
 'scheduler/dequeued': 10,
 'scheduler/dequeued/memory': 10,
 'scheduler/enqueued': 10,
 'scheduler/enqueued/memory': 10,
 'start_time': datetime.datetime(2016, 3, 8, 14, 16, 30, 427657)}
2016-03-08 15:16:31 [scrapy] INFO: Spider closed (finished)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Scrapy 获取网站时出现错误“DNS 查找失败” 的相关文章

随机推荐

  • npm 安装错误:“主机密钥验证失败。”

    我想从 Bitbucket 获取该模块 我在 Windows 服务器上构建了该模块 但是当我使用时出现错误npm install npm ERR Error while executing npm ERR C Users AppData L
  • 如何在访问期间知道ANTLR解析器当前处于哪个替代规则

    如果我们查看 bash 源代码 特别是 yacc 语法 我们可以看到所有重定向都是这样定义的 redirection GREATER WORD LESS WORD NUMBER GREATER WORD NUMBER LESS WORD R
  • 如何让 date_part 查询命中索引?

    我还没有能够让这个查询命中索引而不是执行完整扫描 我有另一个查询 它对几乎相同的表使用 date part day datelocal 该表的数据稍微少一些 但是相同的结构 并且将命中我在 datelocal 列上创建的索引 这是一个没有时
  • C# - 如何使用 TaskSchedular 类列出特定用户的计划任务

    我想知道是否有人可以帮助我 我正在尝试使用 TaskScheduler 类 http www codeproject com KB cs tsnewlib aspx 列出本地计算机上特定用户 管理员 的计划任务我有以下内容 richText
  • 剪一段阿拉伯字符串

    我有一个阿拉伯语字符串 例如 现在我需要剪切这个字符串并输出它 如下所示 我尝试了这个功能 function short name str limit if limit lt 3 limit 3 if strlen str gt limit
  • 从 codecommit 获取私人仓库

    我是 golang 新手 我们正在尝试在 go 中创建一个包并在我们想要使用的所有服务中使用 我尝试在 github 中创建一个存储库并尝试执行 go get 我没有遇到任何问题 现在我想在亚马逊的codecommit中创建相同的包 我将
  • WPF Listview:列重新排序事件?

    当用户更改顺序时 我需要同步两个 ListViews 事件的列顺序 但似乎没有列重新排序事件 目前我只是做了一个AllowsColumnReorder False 但这不是一个永久的解决方案 在网上搜索时 发现很多人都有同样的问题 但没有解
  • 膨胀类 android.widget.ImageButton 时出错

    当我在 系统 应用程序 上安装程序时出现错误 当我使用 数据 应用程序 时 它运行良好 这是错误 android view InflateException Binary XML file line 19 Error inflating c
  • 检查 BIT 列时 LINQ 生成奇怪的 SQL

    我有以下 LINQtoSQL 语句 from t1 in table1 join t2 in table2 on t1 Id equals t2 OtherTableId where t2 BranchId branchId t1 IsPe
  • 在egrep中匹配As后跟相同数量的B

    假设我想匹配一个具有完全相同数量的字符 A 和 B 的模式 这样正好有 n 个 A 后跟 n 个 B 例如 可以匹配以下字符串 AB AABB AAABBB 另一方面 这些字符串无法匹配 BA AAABB AABBB ABAB 为了解决这个
  • SVN 提交未完成

    当我在 svn 中提交文件时 我经常遇到这样的情况 在传输完所有文件后 svn 将挂起 然后最终超时并出现错误svn E175012 Connection timed out 当我上传超过 20 个文件时 似乎会发生这种情况 我相信这是在所
  • C - 将字符串拆分为字符串数组

    我不完全确定如何在 C 中执行此操作 char curToken strtok string curToken ls l we will say I need a array of strings containing ls l and N
  • c++ static_assert 在“if constexpr 语句”的两个分支上均失败

    我试图在编译时确定特定类型是否属于类型标准 对 当我编译下面的代码时 两个分支 即 HERE1 和 HERE2 上的断言均失败 如果我删除 static asserts 并取消注释打印 我会得到我所期望的 这是 HERE1 的is pair
  • 使用三角形网格纹理,无需读/写图像文件

    这是上一个问题的后续 请参阅在javafx上为三角形网格中的各个三角形着色 我认为这本身就是另一个话题 有没有一种方法 使用javafx 可以让我不必实际将图像文件写入磁盘 或外部设备 来使用纹理 换句话说 我可以使用特定的纹理而不必使用图
  • 加载网页,执行其 JavaScript 并将生成的 HTML 转储到文件

    我需要加载一个网页 执行其 JavaScript 以及标签中包含的所有 js 文件 并将生成的 HTLM 转储到文件中 这需要在服务器上完成 我已经尝试过使用node js和zombie js 但它似乎太不成熟 无法在现实世界中工作 通常
  • C# 在特定情况下使用小数位格式化百分比

    在我正在构建的应用程序中 我需要按以下方式格式化百分比 00012 gt 0 01 0012 gt 0 12 012 gt 1 2 12 gt 12 1 12 gt 112 小于 1 的百分比应显示 2 位小数 任何 1 或大于 1 的值都
  • 动态加载数据到Gridview

    当我在 gridview 上工作时 我遇到了以下问题 任何帮助将不胜感激 当我将数据加载到 gridview 时 它仅加载数组的前 3 个项目 但还有 18 个项目需要加载 为什么它不加载其他 15 个项目 Log i 显示了我的 LogC
  • 使用 .AddIdentityServerJwt() 时,.NET Core Razor Pages 应用程序的身份验证不适用于没有“/Identity”路由的视图

    使用 NET Core 3 1 框架 我尝试使用以下设置配置 Web 平台 Razor Pages 应用程序 充当平台的登陆页面 具有平台广告 cookie 同意 隐私政策 联系人以及身份附带的页面 例如登录 注册 管理帐户 等功能 页面
  • 如何在 htaccess 中的 #ancors 和 ?queries 之前从 ulrs 中删除 *.php、index.php 和尾部斜杠

    我无法为我的问题找到令人满意的答案 已经上网冲浪三天了 但没有发现任何实际有效的东西 我的网站结构如下 data controllers helpers partials layouts images javascripts stylesh
  • Scrapy 获取网站时出现错误“DNS 查找失败”

    我正在尝试使用 Scrapy 获取 DNS 查找失败 网站上的所有链接 问题是 每个没有任何错误的网站都打印在解析对象方法 但当 url 返回 DNS 查找失败时 回调parse obj 没有被调用 我想获取所有出现错误的域 DNS 查找失